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
Script to generate text file from excel file isingh786 UNIX for Dummies Questions & Answers 1 01-24-2008 10:32 AM
Read text file from a specified string to the end bsrajirs UNIX for Advanced & Expert Users 9 01-11-2008 01:05 AM
Looking for command(s)/ script to find a text string within a file wrwelden Shell Programming and Scripting 5 11-23-2006 12:53 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 08:53 AM
Generate a string Tux_Raju Shell Programming and Scripting 5 07-29-2005 04:52 AM

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 10-07-2007
walterwaston walterwaston is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 2
generate new string from a text file

Hi,

I have a file and the content looks like this:

line1
line2
File #5 : found
'/u01/testing.txt'
line5
line6
line7
File #12 : found
'/u01/testabc.txt'
line10
line11

I want to write a bash script to give me the output:

The file 5 is '/u01/testing.txt'
The file 12 is '/u01/testabc.txt'


As I am new in shell script, can anyone tell me how to do this?

Thanks!!
Walter
  #2 (permalink)  
Old 10-07-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
try this

Code:
sed -n '/found/{p;n;p;}' filename | tr '\n' ' ' | sed -e 's/ : found/ is /g' -e 's/File/\nFile/g'
  #3 (permalink)  
Old 10-07-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
awk '/File #/{printf "%s %s is ",$1,$2;getline;print}' filename
Code:
sed -n '/File #/{N;s/ : found\n/ is /p;}' filename

Last edited by radoulov; 10-16-2007 at 03:46 PM.. Reason: sed added
  #4 (permalink)  
Old 10-07-2007
walterwaston walterwaston is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 2
Thanks!
It Works!
  #5 (permalink)  
Old 10-13-2007
springs springs is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 4
can you please give idea how you people are writing prog using sed and awk.
i tried several time and but failed.
please give me idea how start.

i read lot of stuff in sed and awk , even thu after seeing this i feel still i lag some where.

any help would be great full
  #6 (permalink)  
Old 10-16-2007
JimJ JimJ is offline
Registered User
  
 

Join Date: Oct 2007
Location: Midwest
Posts: 12
I second that

Jacoden and Radouluv, you guys are really good at this stuff.

If you have a moment to break down the thought process behind some of your solutions, I know that many people would benefit.

Thanks
  #7 (permalink)  
Old 10-16-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
awk

Code:
awk '/File #/{printf "%s %s is ",$1,$2;getline;print}' filename
Code:
/File #/{printf "%s %s is ",$1,$2;
If the record matches the pattern "File #", print (formated without a trailing new line: "%s %s is ") fields #1 and 2 (default field separator - space* - is assumed), so in our case the first match returns:

Code:
File #5 is
Code:
getline;print}
Read the next record (getline) and print it. So from:

Code:
File #5 : found
'/u01/testing.txt'
we get:

Code:
File #5 is '/u01/testing.txt'
sed

Code:
sed -n '/File #/{N;s/ : found\n/ is /p;}' filename
Code:
sed -n '/File #/{N;
1. The -n option: suppress automatic printing of pattern space.
2. If the current record/line matches the pattern "File #"...
3. N; - Read/append the next line of input into the pattern space,
so now, after the first match, our pattern space contains:

Code:
File #5 : found
'/u01/testing.txt'
Code:
s/ : found\n/ is /
In the pattern space: substitute the pattern ": found
" with " is ".

Code:
p;}
Print the current pattern space.

P.S. I modified my original sed version (a small fix).
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 02: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