Use of "CUT" command. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Use of "CUT" command.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-06-2006
sendhil sendhil is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 17 February 2006, 12:09 AM EST
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 02-06-2006
gauravgoel gauravgoel is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 9 July 2008, 4:51 AM EDT
Location: India
Posts: 218
Thanks: 0
Thanked 1 Time in 1 Post
Using awk:

first=awk -F "," '{print $1}'
second=awk -F "," '{print $2}'
third=awk -F "," '{print $3}'
Sponsored Links
    #3  
Old 02-06-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 28 July 2014, 9:39 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
Using shell:

Code:
#!/usr/bin/ksh

oldIFS=$IFS
IFS=$IFS,
while read first second third; do
        echo $first $second $third
done < test
IFS=$oldIFS


Code:
# cat test
1,abc,24
2,def,25
3,pqr,40
# ./test.sh
1 abc 24
2 def 25
3 pqr 40

    #4  
Old 02-06-2006
sendhil sendhil is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 17 February 2006, 12:09 AM EST
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 02-06-2006
gauravgoel gauravgoel is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 9 July 2008, 4:51 AM EDT
Location: India
Posts: 218
Thanks: 0
Thanked 1 Time in 1 Post
can u give a sample output as well
Sponsored Links
    #6  
Old 02-06-2006
sendhil sendhil is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 17 February 2006, 12:09 AM EST
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Output

Hi,
The format of the output should be:
1
2
3

Mani
San
Tom

23
24
25
Sponsored Links
    #7  
Old 02-06-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 28 July 2014, 9:39 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 09:13 PM.