|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Use of "CUT" command.
Hi,
I have 3 fields in a file. For example I have them like this: 1,Santosh, 24 I want to have these 3 values in 3 different variables. How can I do it. Thanks in advance |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Using awk:
first=awk -F "," '{print $1}' second=awk -F "," '{print $2}' third=awk -F "," '{print $3}' |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
Using shell: Code:
#!/usr/bin/ksh
oldIFS=$IFS
IFS=$IFS,
while read first second third; do
echo $first $second $third
done < test
IFS=$oldIFSCode:
# cat test 1,abc,24 2,def,25 3,pqr,40 # ./test.sh 1 abc 24 2 def 25 3 pqr 40 |
|
#4
|
|||
|
|||
|
Regarding AWK
Hi,
Since my file that has got input values in the format "1,Mani,23" and I dont know how many such inputs are going to be there.I wrote the following script to print How many however values were there in the file: while read line do count=1 more values | awk '{print $count}' count=`expr $count + 1` done < values It gives me an error as following: awk: Field $() is not correct. How can print all the values in the file field by field ie... if inputs are like this 1,Mani,23 2,San,24, 3,Tom,25 .. .. and so on. How can I print the first field,2nd field and the third field seperately using awk in a loop Thanx in advance |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
can u give a sample output as well
|
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Output
Hi,
The format of the output should be: 1 2 3 Mani San Tom 23 24 25 |
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
You cannot use shell variables inside an awk command directly. Read up on awk programming to know how to do this. And since you are so keen on using awk to read the variables, try this: Code:
#!/usr/bin/ksh
awk -F',' '{print $1," ",$2," ",$3}' test|while read first second third; do
echo $first $second $third
done |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unix "look" Command "File too large" Error Message | shishong | UNIX for Dummies Questions & Answers | 14 | 05-30-2011 03:47 PM |
| awk command to replace ";" with "|" and ""|" at diferent places in line of file | shis100 | Shell Programming and Scripting | 7 | 03-16-2011 08:59 AM |
| Command Character size limit in the "sh" and "bourne" shell | Roshan1286 | Shell Programming and Scripting | 1 | 10-29-2009 07:38 AM |
| Command Character size limit in the "sh" and "bourne" shell | Roshan1286 | UNIX for Advanced & Expert Users | 1 | 10-29-2009 07:01 AM |
| Command Character size limit in the "sh" and "bourne" shell | Roshan1286 | UNIX for Dummies Questions & Answers | 1 | 10-29-2009 07:01 AM |
|
|