- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Scripting help
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
Forums
Discussions
Discussions
Discussions
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
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
05-29-2005 03:20 AM
05-29-2005 03:20 AM
I need some help with a shell script.
I have the need to parse a file and set vars to what's in the file.
cat input.lst|while read VAR1 VAR2 rest
This works great except in some cases VAR1 can contain spaces. So in those situations VAR1 gets set to everything before the space and VAR2 to everything after.
Is there a way to define the delimiter? I tried enclosing VAR1 in ' and " in the input file with no change in output.
TIA and points for all responses.
Sean
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2005 03:41 AM
05-29-2005 03:41 AM
Re: Scripting help
If you can put a delimiter (e.g. : ) between VAR1, VAR2 and rest; so try
cat input.lst.with.delim| awk -F: '{print $1,$2}' | while read VAR1 VAR2; do
Good luck
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2005 04:09 AM
05-29-2005 04:09 AM
Re: Scripting help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2005 04:45 AM
05-29-2005 04:45 AM
Solutionusually IFS is set to white space (blank, tab,newline). so if you have different delimiter you can set to something else. but you need to be careful because IFS is NOT used only with read. There it is best to set it back.
so you can do something like this:
oldIFS="$IFS"
IFS=","
cat input.lst|while read VAR1 VAR2 rest
IFS="$oldIFS"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2005 05:37 AM
05-29-2005 05:37 AM
Re: Scripting help
ALL available shells have redirection. That saves you a lot of system resources
NEVER DO
cat file | process
BUT
process < file
Then, all `real' scripting languages support a (input) field seperator. For both awk and perl, it's the '-F' option:
lt09:/home/merijn 110 > cat xx.txt
foo,bar,baz
,bogus,fries
kulu,,yuk
blah,ghur,
,,castor
pollux,,
lt09:/home/merijn 111 > awk -F, '{print $2}' xx.txt
bar
bogus
ghur
lt09:/home/merijn 112 > perl -aF, -nle'print $F[1]' xx.txt
bar
bogus
ghur
lt09:/home/merijn 113 >
Note here that in both cases, I didn't even use a redirection for the input. Both scripting languages take the file(s) to be processed as command line argument(s).
If the delimiting is becoming more complex, don't use -F. If for example, the VAR 1 and VAR2 are defined as to be delimited by the last whitespace on the line, perl would be like
# perl -nle'($VAR1,$VAR2)=(m/^(.*\S)\s+(\S+)$/);print$VAR2,$VAR2' file.txt
Enjoy, Have FUN! H.Merijn