Whats wrong with this line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Whats wrong with this line?
# 1  
Old 07-14-2008
Question Whats wrong with this line?

I have a file $I_FILE that I need to filter and store the 1st and the 9th columns in another file $O_FILE.
With this in Perl,

system ("awk -F, '{print \$1, \$9}' \$I_FILE | sed '\/^\$\/d' > O_FILE");

I get:
4096045055

The first line in I_FILE is:
4294967295,0,3,2103159,54668771,54668771,0,3233,566547,28,18,12

so it should print:
4294967295 566547
in o_file.

Thanks!
# 2  
Old 07-14-2008
sorry this:
*****
I get:
4096045055

*****

was from another stray print command, irrelevant here.
# 3  
Old 07-14-2008
Tools Are you sure about your input?

I cannot re-create, nor imagine, where your output is coming from. Can you make a one-line copy of I_FILE and run against that?
Also, unclear what you are trying to accomplish with your sed command.

Code:
#! /usr/bin/perl
system ("awk -F, '{print \$1,\$9}' I_FILE");
exit

Code:
> extr_flds 
4294967295 566547

# 4  
Old 07-14-2008
$I_FILE="ifile";
where ifile looks like this
4294967295,0,3,2103159,54668771,54668771,0,1,566547,8770,0,384,228,18,12
4294967295,0,4,2103160,54668772,54668772,0,1,566548,8770,0,384,228,18,12
4294967295,1,0,2103161,54668773,54668773,0,3,566549,8770,0,384,228,18,12
4294967295,2,0,2103162,54668774,54668774,0,5,566550,8770,0,384,228,18,12

I need to filter out the first and the ninth column:
4294967295 566547
4294967295 566548
4294967295 566549
4294967295 566550

hence:
system ("awk -F, '{print \$1, \$9}' \$I_FILE | sed '\/^\$\/d' > O_FILE")

I dont *have* to use awk/sed. If there is a simpler way with Perl, that would work too.
Thanks!
# 5  
Old 07-14-2008
Question can you use the unix cut command?

Code:
cat I_FILE | cut -d"," -f1,9

or, if you must remain in perl...

Code:
#! /usr/bin/perl

system ("awk -F, '{print \$1,\$9}' I_FILE");

system ("cat I_FILE | cut -d',' -f1,9");
exit

both of the above inside of perl return the 1st & 9th fields.

I still do not understand the sed part - your second half of the command - what are you trying to accomplish?
# 6  
Old 07-14-2008
Or, staying in perl and assuming I_FILE and O_FILE are already opened what's wrong with...

while (<I_FILE>){
#$_ now has contents of line
chomp $_;
(@I_FIELDS) = split(","); #splits $_ on comma
print O_FILE "$I_FIELDS[0] $I_FIELDS[8]\n"; # fields 1 and 9
}

OK, so there's a loop here vs a one-line system() call in the other solutions. I don't know what would perform faster... Might depend on size of I_FILE
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Whats wrong with this if-else

hi whats wrong in below?? CHECK=M10; if ; then echo "hello hi"; else echo "how are u hello hi"; fi I am getting error as ./test.sh: line 2: ' ./test.sh: line 2: M10: command not found ./test.sh: line 2: M10: command not found ./test.sh: line 2: M10: command not found (8 Replies)
Discussion started by: skyineyes
8 Replies

2. Homework & Coursework Questions

Whats wrong with the following

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: ls -ld htdocs drwxr-x--- 3 root root 8192 2006-11-19 10:41 htdocs How would a host administrator... (1 Reply)
Discussion started by: Larry_1
1 Replies

3. Shell Programming and Scripting

Whats wrong in the Function ?

Need your assistance, to find the bug in the function. Function usage erroring out even after passing parameters. usage() { if || ; then echo "************************************************************" echo " CHECK USAGE FOR CORRECT PARAMETERS ... (26 Replies)
Discussion started by: raghunsi
26 Replies

4. UNIX for Dummies Questions & Answers

whats wrong with this?

can anyone tell me why this code doesn't work how its supposed to, its the hangman game but it doesn't play how its supposed to #!/bin/bash NoAttempts="0" livesgiven="5" LivesRemain=$livesgiven LettersAttempted="" wordfile=words numwords=0 function menu() { clear cat << menu... (1 Reply)
Discussion started by: ferrycorsten73
1 Replies

5. Shell Programming and Scripting

whats wrong with this line using perl

E:\>perl -00ne 'push @a,"$_\0$ARGV\n";END{print reverse split/\0/ for sort @a}' file1-obj_prof.out.txt file2-obj_prof.out.txt' Can't find string terminator "'" anywhere before EOF at -e line 1. (6 Replies)
Discussion started by: richsark
6 Replies

6. Shell Programming and Scripting

tell me whats wrong with this

#! /bin/bash USAGE=" | ] if then echo "$USAGE" exit 1 fi while getopts lb: OPTION do case $(OPTION)in a) echo Hi there! exit 2;; b) echo hello o) OARG=$OPTARG;; \?)echo "$USAGE" ;; exit 2;; esac done shift `expr... (1 Reply)
Discussion started by: nadman123
1 Replies

7. Shell Programming and Scripting

tell me whats wrong in this?

#! /bin/bash head -5 $1 echo "remove $1 ?" read answer if then echo invalid answer elif rm $1 echo "$1 is deleted" elif then echo file is not deleted else echo "invalid answer" fi What i really want this to do is to ask to delete the file or not..it says something wrong... (1 Reply)
Discussion started by: nadman123
1 Replies

8. Shell Programming and Scripting

Whats wrong with this script?

Hi all, #!/bin/ksh BIN=/interface/Gunner age=$1 directory="$2" && directory=. cd "$directory" || exit 1 from=`$BIN/today -$age` cd $BIN for i in `cat filestoarchive.txt`;do cd $i find . -mtime 14 | grep -v '.tar$' | $BIN/dttmfilter | awk '$1<="'$from'"{ print;};' | \ done (2 Replies)
Discussion started by: kayarsenal
2 Replies

9. Shell Programming and Scripting

Whats wrong with this 5 line script!

Hi #!/bin/sh user=$1 if " -eq 0 ] echo "No" else echo "Yes" fi I'm not quite sure whats wrong with this but I know its something silly. Any ideas? Thanks (9 Replies)
Discussion started by: c19h28O2
9 Replies

10. Shell Programming and Scripting

whats wrong with this awk???

while read LINE; do echo |awk -v LIN="${LINE}" '{print substr(LIN,1,7)","substr(LIN,8,5)" ,"substr(LIN,14,10)","substr(LIN,24,6)" ,"substr(LIN,30,8)}'; done < exp1.txt exp1.txt = 1234 grgrg 203902 ksdjh oehr weo why it doesn't work?? thanks. (5 Replies)
Discussion started by: george_
5 Replies
Login or Register to Ask a Question