LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-09-2006, 02:10 PM   #1
anaik100
LQ Newbie
 
Registered: Nov 2006
Posts: 5

Rep: Reputation: 0
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.
 
Old 11-09-2006, 03:27 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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)
 
Old 11-10-2006, 09:20 AM   #3
anaik100
LQ Newbie
 
Registered: Nov 2006
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 11-10-2006, 10:46 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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
ne/two
$ echo "one/two/three/four/five" |cut -d/ -f3,5
three/five
See "man cut" for more details.

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
Almost/but/not/quite/entirely/unlike/tea
All/of/the/true/things/that/I/am/about/to/tell/you/are/shameless/lies
The/old/Lie:/Dulce/et/decorum/est/Pro/patria/mori
$ cat testfile | while IFS=/ read var1 var2 var3; do echo "var1=$var1 var2=$var2 var3=$var3"; done
var1=Almost var2=but var3=not/quite/entirely/unlike/tea
var1=All var2=of var3=the/true/things/that/I/am/about/to/tell/you/are/shameless/lies
var1=The var2=old var3=Lie:/Dulce/et/decorum/est/Pro/patria/mori
As you can see, if you only provide 3 variables but there are more fields in the input line, the last variable gets all the rest of the line which is not assigned yet. If you provide more variables than there are fields, the extra variable will be assigned with empty strings.

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
$ INFO=$(basename $FILE)
$ echo "$INFO"
trapped.in.the.amber.of.the.moment.there.is.no.why
$ FILENAME=$(echo "$INFO" |cut -d. -f8-11)
$ echo "our nice new filename: $FILENAME"
our nice new filename: there.is.no.why
Notes:
  1. I used $(basename "$FILE") instead of `basename "$FILE"`. It does the same thing but it's less likely to be mis-copied (lots of people use the wrong quote - ' instead of `). It also has the advantage that you can use a $() inside of another $(), whereas it's not possible to nest `backtick execution`.
  2. There's another way to do basename which is less readable, but slightly faster. basename is a separate program from the shell which means the OS has to execute a new process, get the output, then close the process etc. All this takes some time. Linux us actually remarkably fast at this compared to other OSes, but even so, if you can do the same thing "inside" the shell process, it'll be lighter on your machine. The only dis-advantage is that the in-shell version is a bit less readable. Here it is anyhow ${FILE##*/}. Read the Parameter Expansion section of the bash manual page to understand this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script questions tgreiner Linux - Newbie 6 04-05-2005 01:00 AM
Script questions, please help Joey^s Programming 4 11-29-2004 02:53 AM
script questions shaoshuai97 Linux - Certification 2 10-12-2004 10:00 AM
two shell script questions tutwabee Linux - General 4 08-10-2004 06:03 PM
Shell Script Questions Col Panic Linux - General 13 10-12-2003 12:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration