linux script questions
Hi,
1. I want to copy the number of lines in a file to a variable , but the command linecount='wc -l $FTP_dir/deletedata.list' is giving me an error. It says -l is invalid symbol 2. I have a file which as mentioed below /arsload/linedata/FAILURE/MLQ340TD.LINE.MLQ340TD.MLQ340TA.20061023.191719.ARD I want to split this file, so that any name that exists after / or . can be stored in individual variable I tried N1='echo $INFO | awk '{split{$1,arr,"/"}; print arr[1] }'' N2='echo $INFO | awk '{split{$1,arr,"/"}; print arr[2] }'' N3='echo $INFO | awk '{split{$1,arr,"/"}; print arr[3] }'' N4='echo $INFO | awk '{split{$1,arr,"/"}; print arr[4] }'' FN1='echo $INFO | awk '{split{$1,arr,"."}; print arr[5] }'' FN2='echo $INFO | awk '{split{$1,arr,"."}; print arr[6] }'' FN3='echo $INFO | awk '{split{$1,arr,"."}; print arr[7] }'' FN4='echo $INFO | awk '{split{$1,arr,"."}; print arr[8] }'' FN5='echo $INFO | awk '{split{$1,arr,"."}; print arr[9] }'' FN6='echo $INFO | awk '{split{$1,arr,"."}; print arr[10] }'' but i feel this process is wrong. Can anybody please correct me. |
1a. Were you using the apostrophe, ' or the backtick, `? It's not easy to see the difference, especially with some fonts, but you'll not get the right behavior if you get the wrong one. There is an alternative syntax, which I prefer in part because it's not so common to make this mistake... instead of `command`, use $(command).
1b. wc -l filename will output the linecount and the filename. You can prevent this by passing the file on standard input: Code:
linecount=$(wc -l < file.dat) |
thanks matt...
can you please update me on second problem.. my logic is i=0 linecount=`wc -l $FTP_dir/deletedata.list | awk '{print $1}'` while [ $linecount -eq $i ] do for FILE in `cat ${FTP_dir}/deletedata.list` do INFO=`basename $FILE` N1=`echo $INFO | awk '{split{$1,arr,"/"}; print arr[1] }'` N2=`echo $INFO | awk '{split{$1,arr,"/"}; print arr[2] }'` N3=`echo $INFO | awk '{split{$1,arr,"/"}; print arr[3] }'` N4=`echo $INFO | awk '{split{$1,arr,"/"}; print arr[4] }'` FN1=`echo $INFO | awk '{split{$1,arr,"."}; print arr[5] }'` FN2=`echo $INFO | awk '{split{$1,arr,"."}; print arr[6] }'` FN3=`echo $INFO | awk '{split{$1,arr,"."}; print arr[7] }'` FN4=`echo $INFO | awk '{split{$1,arr,"."}; print arr[8] }'` FN5=`echo $INFO | awk '{split{$1,arr,"."}; print arr[9] }'` FN6=`echo $INFO | awk '{split{$1,arr,"."}; print arr[10] }'` FILENAME=$FN1.$FN2.$FN3.$FN4.$FN5.$FN6 if [ $FN1 -eq $SN1 ] then rm -f ${FILENAME} SN1=$FN1 else SN1=$FN1 fi done # end of for loop i = `expr $i + 1` done # end of while loop |
Splitting up a line into fields can be done in several ways. If you just want one particular field, the cut command is useful. It can split by a delimiting character or by character ranges, e.g.
Code:
$ echo "one/two/three/four/five" |cut -c2-7 Another approach is to tell the shell's read function to split the line. The splitting is usually done on whitespace (spaces and tabs), but you can set the IFS variable. You can set this just before calling read, and it is only set for that operation: Code:
$ cat testfile See "man bash" for more details. The only caveat here is that the bash manual page is so huge that finding the correct section is a pain in the bum. If you have the same version of the manual page that I have, you might try searching for "-n nchars", which takes me to the correct section in one go. In your script you seem to want to take the part after the last /, split it by the period character, and use fields 5-10 to make FILENAME. You can get the last part of a path using basename (which you actually do, but don't use it), then use cut to construct the fields 5-10 part. For example: Code:
$ FILE=/here/we/are/trapped.in.the.amber.of.the.moment.there.is.no.why
|
All times are GMT -5. The time now is 06:34 PM. |