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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to open program and send/execute command in program tuathan UNIX for Dummies Questions & Answers 1 11-19-2008 09:59 PM
A program to trace execution of another program jiten_hegde High Level Programming 3 08-19-2008 05:26 AM
How to write to stdin of another program (program A -> [stdin]program B) vvaidyan UNIX for Dummies Questions & Answers 3 08-02-2008 05:21 PM
How to write to stdin of another program (program A -> [stdin]program B) vvaidyan High Level Programming 1 04-30-2008 01:44 PM
executing a program within a program Gekko High Level Programming 4 06-27-2002 03:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-09-2009
sivakumar.rj sivakumar.rj is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 74
Question AWK program

Hi all,

I need to grep the 3 characters from a file, and to fetch the corresponding words to that character. My file is in the following format..

The below text will be in the separate file....say file2.txt

ABC This is the first text.
DEF This is the second text.
GH1 9.8.7890
AB1 This is the first1 text.
DE2 This is the second1 text.
GH2 19.8.7890
AB3 This is the first2 text.
DE3 This is the second2 text.
GH3 29.5.7890
AB4 This is the first3 text.
DE4 This is the second4 text.
GH4 9.9.7890

Now I need to grep for GH1, 2, 3 ,4 records and extract the corresponding number upto 5 position..

i.e GH1 9.8.7, GH2 19.8.7...like this so on....and I have to add these number into another along with a text to the top. For example, in my file1.txt...I need to add like this...

The world is round 9.8.7
Today is my birthday 19.8.7

like this...I need to do it...Pls help on this...
  #2 (permalink)  
Old 03-09-2009
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Code:
BEGIN {
	FS="[ .]"
}

/GH[12]/ {
	str = $1=="GH1" ? "The world is round" : "Today is my birthday"
	printf "%s %s.%s.%.1s\n", str, $2, $3, $4
}
Or the same as a one-liner:
Code:
awk -F'[ .]' '/GH[12]/ {str=$1=="GH1"?"The world is round":"Today is my birthday";printf "%s %s.%s.%.1s\n",str,$2,$3,$4}' your.file
Sponsored Links
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 06:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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