- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Query on scripting
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-20-2009 12:12 AM
тАО12-20-2009 12:12 AM
(A) If I have a file called data that contains the following:
abc
def
ghi
I can use script to perform task like:
for i in `cat $data`
do
echo $i
done
(B) What if the file contains the following:
abc 123
def 456
ghi 789
How to reference the first column data and second column data if I would like to perform the same thing as (A) in shell script?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-20-2009 12:20 AM
тАО12-20-2009 12:20 AM
Solutionfor i in $(< $data); do
>How to reference the first column data and second column data if I would like to perform the same thing as (A) in shell script?
You can use while read:
while read first second dummy; do
echo "$first $second"
done < data
"dummy" eats up any third, etc columns.
- Tags:
- evil cat
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-20-2009 12:28 AM
тАО12-20-2009 12:28 AM
Re: Query on scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-20-2009 04:50 PM
тАО12-20-2009 04:50 PM
Re: Query on scripting
In addition, you can also do something like:
cat ${file | while read line
do
echo "${line}"
done
Note the quotes around the variable. That syntax comes in handy if your datafile has lines with one parameter, others with two or more...
Once you have the "line" in ${line}, you can then parse it out however you want.
HTH;
Doug
------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-20-2009 06:29 PM
тАО12-20-2009 06:29 PM
Re: Query on scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-20-2009 07:16 PM
тАО12-20-2009 07:16 PM
Re: Query on scripting
> Where should the "}" put in
> cat ${file | while read line
cat ${file}
Again, "cat" is not really needed. For
example:
bash$ cat sh1.sh
#!/bin/sh
(
while read line ; do
echo "${line}"
done
) < $1
bash$ ./sh1.sh sh1.sh
#!/bin/sh
(
while read line ; do
echo "${line}"
done
) < $1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-21-2009 05:05 AM
тАО12-21-2009 05:05 AM
Re: Query on scripting
> Where should the "}" put in cat ${file | while read line
Doug's post had a typographical error in that the closing brace was lost. The syntax he meant to use was:
cat ${file} | while read line
Enclosing shell variables in curly braces to signal parameter substitution is a good practice to develop. The 'sh-poxix' manpages noted that, "Braces are required when parameter is followed by a letter, digit, or underscore that should not be interpreted as part of its name or when a named parameter is subscripted."
As Dennis first noted, using a 'cat' process to read a file whose input is then piped to a shell 'read' is wasting a process. The shell can do the read without another process and is thus more efficient.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-21-2009 08:10 AM
тАО12-21-2009 08:10 AM
Re: Query on scripting
If I may write my own version which is slightly different than Dennis' :
exec 3< $data
while read -u3 fist second dummy ; do
print "$first $second"
done
exec 3<&-
The advantages are that :
1) it shows which file you're processing since the beginning of the loop;
2) you might open several files at the same time by changing the file descriptor (exec 3< $data 4< $data2 5< $data3).
Cheers,
Jean-Philippe
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-21-2009 05:21 PM
тАО12-21-2009 05:21 PM
Re: Query on scripting
What does 3 means?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-21-2009 05:43 PM
тАО12-21-2009 05:43 PM
Re: Query on scripting
File number 3. The next free one after stdin(0), stdout(1) and stderr(2).
(Perhaps it is better to leave well enough alone and skip the more fancy exec stuff.)
>Jean-Philippe: 1) it shows which file you're processing since the beginning of the loop
If that's important, a comment is more descriptive than fancy exec and read -u.
>2) you might open several files at the same time ... (exec 3< $data 4< $data2 5< $data3).
It might be more understandable with one per line.