Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print selected lines

Status
Not open for further replies.

tonivm

Technical User
Mar 2, 2006
64
ES
Hi everybody:

Hi everybody:
I try to print in new file selected lines from another file wich depends on the first column.

I have done a script like this:

Code:
lines=( "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "21" "31" "41" "51" "55" "57" "58" )
${lines[@]}
for lines in ${lines[@]}
do
  awk -v  target=$lines  ' if(NR == target) {print $0} ' file1 >> file2
done

But I have the next error:

/T-eff.sh: 42: Syntax error: "(" unexpected ------ where the line 42 correspond when I declare the array "lines"

Any suggestion?. Thanks in advance. :D
 
Hi

Code:
[gray]# You use bash, right ? Will not work in ksh.[/gray]
lines=( "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "21" "31" "41" "51" "55" "57" "58" )

[gray]# Why this line ? Remove it.[/gray]
[gray]# [red]${lines[@]}[/red][/gray]

[gray]# Use distinct identifiers. This is not Perl.[/gray]
for [red]line[/red] in ${lines[@]}
do
  awk -v  target=[red]$line[/red]  ' if(NR == target) {print $0} ' file1 >> file2
done

Feherke.
 
Hi:
Thanks for your reply.
Code:
# You use bash, right ? ---- Yes I do. 
# Will not work in ksh.
lines=( "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "21" "31" "41" "51" "55" "57" "58" )

# Why this line ? Remove it.
# I did it now.
# ${lines[@]}

# Use distinct identifiers. This is not Perl.
# Of course, I changed it.
for line in ${lines[@]}
do
  awk -v  target=$line  ' if(NR == target) {print $0} ' file1 >> file2
done
But I have the same error.
:D
 
Thakns a lot for replies. I have solved the problem.

At the begining of the script, I have changed:

Code:
#!/bin/sh

for

Code:
#!/bin/bash

And now, it works correctly. Best regards for everybody.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top