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
Can I use sed to insert a string which has colon Jenny.palmy UNIX for Dummies Questions & Answers 2 04-28-2008 08:04 PM
Read text file from a specified string to the end bsrajirs UNIX for Advanced & Expert Users 9 01-11-2008 01:05 AM
Read string from a file,plz help me to check joshuaduan Shell Programming and Scripting 1 05-11-2007 04:12 AM
Failed to insert string into file before line nir_s Shell Programming and Scripting 7 07-26-2005 02:37 AM
vector<string> with insert cmd photon High Level Programming 1 09-10-2004 06:51 PM

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 12-10-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
How to insert a string at the end of a file read

Hi,

This thread is an extension of the post I posted in another folder for Unix Dummies.

I am actually trying to read all the files in a folder, let say folder A.

There are alot of .txt files in this folder where I have sed them and translate to numeric elements.

For example:-
I have 0,0,0,0,0,0,0,0,0,.......

Firstly, I have problem where I would like to insert a string "test" for the last element of each file read; lets say:-

0,0,0,0,0,0,0...........,test

Second, I would like to insert a newline character before next file is read similarly.

Please advise. Thanks.


-Jason
  #2 (permalink)  
Old 12-10-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Smile

Quote:
Originally Posted by ahjiefreak View Post
Hi,

This thread is an extension of the post I posted in another folder for Unix Dummies.

I am actually trying to read all the files in a folder, let say folder A.

There are alot of .txt files in this folder where I have sed them and translate to numeric elements.

For example:-
I have 0,0,0,0,0,0,0,0,0,.......

Firstly, I have problem where I would like to insert a string "test" for the last element of each file read; lets say:-

0,0,0,0,0,0,0...........,test

Second, I would like to insert a newline character before next file is read similarly.

Please advise. Thanks.


-Jason
I am not sure about your requirment..But
I hope END in gawk/nawk script will do your first requirment ..

user_prady
  #3 (permalink)  
Old 12-10-2007
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,241

Code:
awk 'FNR == 1 && NR > FNR { print "" } { print $0 ",test" }' *.txt

  #4 (permalink)  
Old 12-10-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
How about this for recursively modifying files..


Code:
for temp in `find ./A -name  "*.txt"`
do
        gawk -v file=$temp '
              END{printf "test" >>  file}
        ' $temp
done

  #5 (permalink)  
Old 12-10-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Hi reborg,

Thanks for the input. I tried to simulate them when I have a file described.

Could you explain to me what these does ; I understand that
FNR stands for current record number in filename which is 1
However, i do not understood why NR>FNR . Is that for inserting a new line which is {print ""} ?

awk 'FNR == 1 && NR > FNR { print "" } { print $0 ",test" }' *.txt

Actually, my file structure is originally not from the format of 0,0,0,0,1,0,0.

Rather, I have a C file where I extract the first field of all the files
and arranged into the above mentioned format. Below is the code from my original file to the above mentioned format.

cat b*.txt| sed 's/^[ ]*//' |sed 's/^-/0/'|sed 's/^#####/0/'|sed '/^[^0-9-]/d'|tr ':' "," |awk ' {

printf ("%s",$1);

}'>> output.txt

I tried to apply the code that you pointed to me in this scenario but it doesnt allow to work.

Please advise. Appreciate alot. Thanks.

-Jason
  #6 (permalink)  
Old 12-10-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Hi,

from the above code,

I tried something like this:-

cat b*.txt| sed 's/^[ ]*//' |sed 's/^-/0/'|sed 's/^#####/0/'|sed '/^[^0-9-]/d'|tr ':' "," |awk '{printf ("%s",$1);}'| awk 'FNR == 1 && NR > FNR { print "" } { print $0 ",test" }' >>output.txt

However, given I have two files, the "string" of "test" is only appended at the end of reading these files. Any idea how to break the file apart to that new line and "test" can be inserted?Thanks.

-Jason
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 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