- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- parameter substitution - newbie question
Categories
Company
Local Language
Forums
Discussions
Knowledge Base
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Knowledge Base
Forums
Discussions
- Cloud Mentoring and Education
- Software - General
- HPE OneView
- HPE Ezmeral Software platform
- HPE OpsRamp
Knowledge Base
Discussions
Forums
Discussions
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
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-12-2003 04:36 PM
05-12-2003 04:36 PM
Let's say I've assigned "abc123,def456" to $i.
1. How can I use the comma as a delimiter to split $i into $a and $b ($a="abc123"; $b="def456")?
2. How can I retrieve only the first character of $i?
Maybe I should describe what I'm really after, because there's a good chance I'm going about it all wrong.
I've got a text file that looks like this:
first, 1
second, 2
#third, 3
fourth, 4
I'm writing a script that will ignore all lines preceded by # and, for each of the remaining lines, assign everything before the comma to $a, assign everything after the comma to $b, and run a command using both variables.
I've tried it two ways.
The first way works but feels kinda ugly to me:
while read -r a b
do
case $a in
\#*) ;;
*) echo a is $a and b is $b;;
esac
done < $1
I think the second approach is better but I just can't figure out how to split the variable:
export IFS=""
for i in `grep -v "^#" $1`
do
echo full line is $i
done
Any help appreciated!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2003 04:38 PM
05-12-2003 04:38 PM
Re: parameter substitution - newbie question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2003 05:02 PM
05-12-2003 05:02 PM
Re: parameter substitution - newbie question
Here is one way to do it using a variation of your first method:
grep -v "^#" $1 | while read a b
do
echo "a is $a and b is $b"
done
That way, you filter out the lines you don't want before you operate with them.
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2003 05:07 PM
05-12-2003 05:07 PM
Re: parameter substitution - newbie question
Let me try this as an attachment.
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2003 07:47 PM
05-12-2003 07:47 PM
Re: parameter substitution - newbie question
awk -F, '/^[^#]/ {printf("%s %s\n",$1,$2);}' yourFile |
while read a b
do
command $a $b
done
or
if your not interested in the command's return code or processing the output from the command:
awk -F, '/^[^#]/ {system("command "$1 $2"");}' yourFile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2003 08:19 PM
05-12-2003 08:19 PM
Solutionusing IFS(assumes you want first two fields):
oldIFS=$IFS
IFS=","
echo $i | read a b
IFS=$oldIFS
using tr to change ,to space:
cat yourFile |tr -s "," " " | read a b
using sed to change , to space
cat yourFile | sed 's/\(.*\),\(.*\)/\1 \2/ | read a b
my previous example used awk to change , to space.
using cut(assumes you want first two fields):
a=$(echo $i | cut -d, -f1)
b=$(echo $i | cut -d, -f2)
using variable substrings(assumes their are only two fields):
$a=${$i#*,}
$b=${$i%,*}
2. How can I retrieve only the first character of $i?
x=$(echo $i| sed 's/\(.\).*/\1/')
x=$(echo $i| awk '{printf("%s",substr($0,1,1));}')
x=$(echo $i| cut -c 1)
take your pick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2003 08:53 PM
05-12-2003 08:53 PM
Re: parameter substitution - newbie question
2. How can I retrieve only the first character of $i?
typeset -L1 x
x=$i
if you can't do it ten different ways, you're not using uxix or you haven't thought long enough.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2003 05:06 AM
05-13-2003 05:06 AM
Re: parameter substitution - newbie question
And Curt, thanks for the variable substring example; I've been puzzling over substrings all day!