The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
"find command" to find the files in the current directories but not in the "subdir" swamymns Shell Programming and Scripting 9 07-22-2008 12:23 PM
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
Unix "at" / "Cron" Command New Problem...Need help Mohanraj UNIX for Dummies Questions & Answers 3 01-26-2006 08:08 PM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-06-2006
sendhil sendhil is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 13
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
  #2 (permalink)  
Old 02-06-2006
gauravgoel gauravgoel is offline
Registered User
  
 

Join Date: Dec 2005
Location: India
Posts: 218
Using awk:

first=awk -F "," '{print $1}'
second=awk -F "," '{print $2}'
third=awk -F "," '{print $3}'
  #3 (permalink)  
Old 02-06-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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 (permalink)  
Old 02-06-2006
sendhil sendhil is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 13
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
  #5 (permalink)  
Old 02-06-2006
gauravgoel gauravgoel is offline
Registered User
  
 

Join Date: Dec 2005
Location: India
Posts: 218
can u give a sample output as well
  #6 (permalink)  
Old 02-06-2006
sendhil sendhil is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 13
Output

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

Mani
San
Tom

23
24
25
  #7 (permalink)  
Old 02-06-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0