The UNIX and Linux Forums  

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
Read the csv file and assign the values in to variable rajbal Shell Programming and Scripting 11 06-27-2009 02:17 PM
read and assign each character from the string to a variable Tek-E Shell Programming and Scripting 1 01-26-2009 06:12 AM
how can i read text file and assign its values to variables using shell rosalinda Shell Programming and Scripting 4 08-26-2008 06:20 AM
How to Read a config file and Assign to Variable redlotus72 UNIX for Dummies Questions & Answers 1 11-13-2006 12:14 PM
How can I read variable values from file? redlotus72 UNIX for Dummies Questions & Answers 1 03-14-2005 05:38 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 06-09-2009
depakjan depakjan is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
Exclamation Read a file and assign the values to a variable

i have a file in this format

curyymm PRVYYMM CDDMmmYY bddMmmyy eddMmmyy
--------- ------- ------------ ---------- -----------
0906 0905 09Jun09 01Jun09 30Jun09
----------- --------- ------------ ------------ -----------

i need to read the third line and write a new file like this


curyymm = 0906
PRVYYMM = 0905
CDDMmmYY = 09Jun09
bddMmmyy = 01Jun09
eddMmmyy = 30Jun09
  #2 (permalink)  
Old 06-09-2009
funksen funksen is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2006
Location: Austria/Vienna
Posts: 431

Code:
sed -n 3p file1 |  read curyymm PRVYYMM CDDMmmYY bddMmmyy eddMmmyy  

echo "curyymm = $curyymm
PRVYYMM = $PRVYYMM
CDDMmmYY = $CDDMmmYY
bddMmmyy = $bddMmmyy
eddMmmyy = $eddMmmyy" > file2

  #3 (permalink)  
Old 06-09-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Or with awk:


Code:
awk '
NR==1{split($0,a);next}
NR==3{for(i=1;i<=NF;i++){print a[i] " = " $i};exit}
' file > newfile

Regards
  #4 (permalink)  
Old 06-10-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by funksen View Post
Code:
sed -n 3p file1 |  read curyymm PRVYYMM CDDMmmYY bddMmmyy eddMmmyy  

echo "curyymm = $curyymm
PRVYYMM = $PRVYYMM
CDDMmmYY = $CDDMmmYY
bddMmmyy = $bddMmmyy
eddMmmyy = $eddMmmyy" > file2

That will fail in every shell except ksh. It will fail in pdksh, bash, dash, etc..

There's no need for an external command if you want the third line; if it's the 50th (give or take a dozen or two) or higher, use sed.


Code:
{ read;read;
read curyymm PRVYYMM CDDMmmYY bddMmmyy eddMmmyy  
} < file1
echo "curyymm = $curyymm
PRVYYMM = $PRVYYMM
CDDMmmYY = $CDDMmmYY
bddMmmyy = $bddMmmyy
eddMmmyy = $eddMmmyy" > file2

  #5 (permalink)  
Old 06-09-2009
depakjan depakjan is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
wow.. that was very quick.. thank you guys.. it was very helpfull.. i din think this would be this simple.. you guys made my life easy
  #6 (permalink)  
Old 06-10-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,099

Code:
nawk 'NR==1{
	n=split($0,keys," ")
	next
}
NR==3 {
	split($0,vals," ")
	for(i=1;i<=n;i++)
		printf("%s=%s\n",keys[i],vals[i])
	exit
}' a.txt

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 05:49 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