LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-02-2013, 08:46 PM   #16
sysmicuser
Member
 
Registered: Mar 2010
Posts: 340

Original Poster
Rep: Reputation: 0

@chirsm01

Can you shed some light on pan64 post please? that looks to me interesting but I couldn't understand it how he is extracting the string. That is not regex as well is that part of perl?
 
Old 05-02-2013, 08:52 PM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
re - pan64's post, this is still bash. He has used parameter substitution (# and %%) to retrieve the data.

See here for further details
 
1 members found this post helpful.
Old 05-02-2013, 09:42 PM   #18
sysmicuser
Member
 
Registered: Mar 2010
Posts: 340

Original Poster
Rep: Reputation: 0
Thank you grail ! may be I have some more question but a bit later !

Happy shell scripting weekend guys !!!
 
Old 05-02-2013, 11:34 PM   #19
sysmicuser
Member
 
Registered: Mar 2010
Posts: 340

Original Poster
Rep: Reputation: 0
Smile

@suicidaleggroll

You are quite right. There is no issue there, typing mistake ! I tried with small sample script and it works well
Here is the code and output !

Code:
cat ${input_config_file}|while IFS="," read rec1 _ _ _; do echo $rec1 ; done
+ IFS=,
+ read rec1 _ _ _
+ cat /home/oracle/sample.txt
+ echo /h1/h2/h3/h4/input
/h1/h2/h3/h4/input
+ IFS=,
+ read rec1 _ _ _
+ echo /h1/h2/h3/h4/input
/h1/h2/h3/h4/input
+ IFS=,
+ read rec1 _ _ _
+ echo /h1/h2/h3/h4/input.600
/h1/h2/h3/h4/input.600
+ IFS=,
+ read rec1 _ _ _
+ echo /h1/h2/h3/h4/input
/h1/h2/h3/h4/input
+ IFS=,
+ read rec1 _ _ _
+ echo /h1/h2/h3/h4/input
/h1/h2/h3/h4/input
+ IFS=,
+ read rec1 _ _ _
+ echo /h1/h2/h3/h4/input
/h1/h2/h3/h4/input
+ IFS=,
+ read rec1 _ _ _
 
Old 05-05-2013, 10:57 AM   #20
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Code:
cat ${input_config_file}|while IFS="," read rec1 _ _ _; do echo $rec1 ; done
^    ^                                      ^    ^              ^
1    2                                      3    4              5
1) Useless Use Of Cat. The file can be directly fed into the while loop. Saves one more process.

2) The full bracketed form of a variable is unnecessary unless you're doing a parameter substitution or you need to concatenate it with another plain text string. In all other cases it does nothing but clutter up the code, which can even be detrimental by obscuring syntax mistakes. Leave them off when unneeded.

3) When read is given multiple variable names, the input line is split according to the current IFS value and one word is placed into each. Leading and trailing whitespace is removed if IFS contains that character (this happens even with a single variable). If there are more variables than words, the extras remain empty, and if there are more words than variables the last one contains all the excess. This means you only need one instance of it to capture all the extra unwanted text on the line.

You should also generally use the "-r" option with read, to disable interpretation of backslashes in the input text.

4) "_" is a special variable name in most shells. It generally contains the last argument of the most recently run command, and so is overwritten after every execution. This makes it perfect for use as a disposable variable.

And as mentioned, even if you used a regular variable, the amount of extra overhead is so small that it's usually better to use read to split than an external command.

5) QUOTE ALL OF YOUR VARIABLE EXPANSIONS! You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell and any possible globbing patterns expanded. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions when you need them.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


So the correct form of this should be:
Code:
while IFS="," read -r rec1 _; do echo "$rec1" ; done <"$input_config_file"

Last edited by David the H.; 05-05-2013 at 11:02 AM. Reason: small corrections
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LaTeX Table code not working, even example code from book. This is my username Programming 1 12-31-2012 12:00 PM
[SOLVED] Two pieces of code which do the same, one not working? ThatPerson Programming 7 12-11-2011 09:01 AM
code not working namita m Linux - Newbie 4 11-01-2010 02:35 PM
html code not working.... linuxlover.chaitanya Linux - Newbie 16 02-12-2009 01:30 AM
SED - minor changes work - Larger doesn't (working and non working code included) Nimoy Programming 17 09-22-2007 04:34 PM


All times are GMT -5. The time now is 10:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration