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
extract data from file raidzero Shell Programming and Scripting 4 06-17-2008 02:04 PM
How to extract data from a huge file? srsahu75 Shell Programming and Scripting 5 01-18-2008 05:06 AM
Extract data from file getdpg Shell Programming and Scripting 20 08-03-2006 03:54 AM
How to extract data from a text file negixx Shell Programming and Scripting 1 07-19-2005 09:30 PM
extract data from file apalex Shell Programming and Scripting 2 04-27-2002 01:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-18-2008
ps_sach ps_sach is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 13
Smile extract data from file

I m new to shell scripting & i need a help....

i have file like....

Name := sachin
address:=something
phone:=111

...

Note: There might be or not space between Name & := and between := & sachin. I need to extract the data from each line of file as
var1=Name
value1=sachin

same for the others also...
  #2 (permalink)  
Old 09-18-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Code:
awk -F':|= ' '{print $1, $2}' inputfile |\
while read var value
do
   echo "var=$var  value=$value"
done
  #3 (permalink)  
Old 09-18-2008
ps_sach ps_sach is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 13
Smile Problem in getting the out put

Quote:
Originally Posted by jim mcnamara View Post
Code:
awk -F':|= ' '{print $1, $2}' inputfile |\
while read var value
do
   echo "var=$var  value=$value"
done
Thanks for the help.......
But there is problem again. I will explain u..

My file is - inputfile.txt
Name := sachin
phone := 111
address:=something

By using u r code i m getting o/p as....
var=Name value=
var=phone value=111
var=address value=
I think u r $value veriable is not doing the desired thing......
..........Please give me the solution ......i m waiting for the reply.....
  #4 (permalink)  
Old 09-18-2008
manosubsulo manosubsulo is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 87
u need to do small change in the first line as

Old : awk -F':|= ' '{print $1, $2}' inputfile.txt |\
New : awk -F\:= '{print $1, $2}' inputfile.txt |\
  #5 (permalink)  
Old 09-18-2008
ps_sach ps_sach is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 13
Smile

Quote:
Originally Posted by manosubsulo View Post
u need to do small change in the first line as

Old : awk -F':|= ' '{print $1, $2}' inputfile.txt |\
New : awk -F\:= '{print $1, $2}' inputfile.txt |\
Thanks for the help , but again the o/p is-


Name val=sachin
var=phone val=111
var=address val=something
waiting for the reply......
  #6 (permalink)  
Old 09-18-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool Are you looking to head in this direction?

The input file
Code:
> cat file1a
Name := sachin
address:=something
phone:=111
The program script
Code:
> cat mkfile2a
sed "s/Name/~Name/g" file1a | tr "\n" "|" | tr "~" "\n" | tail +2>file2a
echo "" >>file2a

while read zf
   do
   echo $zf
   var1=$(echo $zf | cut -d"|" -f1 | cut -d":" -f1 | tr -d " ")
   val1=$(echo $zf | cut -d"|" -f1 | cut -d"=" -f2 | tr -d " ")
   var2=$(echo $zf | cut -d"|" -f2 | cut -d":" -f1 | tr -d " ")
   val2=$(echo $zf | cut -d"|" -f2 | cut -d"=" -f2 | tr -d " ")
   var3=$(echo $zf | cut -d"|" -f3 | cut -d":" -f1 | tr -d " ")
   val3=$(echo $zf | cut -d"|" -f3 | cut -d"=" -f2 | tr id " ")

   echo "Var1="$var1
   echo "Value1="$val1
   echo "Var2="$var2
   echo "Value2="$val2
   echo "Var3="$var3
   echo "Value3="$val3

done<file2a
The output from execution (the input is echo'd to screen just as a debug tool)
Code:
> mkfile2a
Name := sachin|address:=something|phone:=111|
Var1=Name
Value1=sachin
Var2=address
Value2=something
Var3=phone
Value3=111
  #7 (permalink)  
Old 09-19-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Code:
sed 's/ //g' filename | nawk -F":=" '{
print "var"NR"="$1
print "value"NR"="$2
}'
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 10:23 PM.


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