How to insert a string at the end of a file read


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert a string at the end of a file read
# 1  
Old 12-10-2007
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  
Old 12-10-2007
Bug

Quote:
Originally Posted by ahjiefreak
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  
Old 12-10-2007
Code:
awk 'FNR == 1 && NR > FNR { print "" } { print $0 ",test" }' *.txt

# 4  
Old 12-10-2007
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  
Old 12-10-2007
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  
Old 12-10-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

2. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

3. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

4. UNIX for Dummies Questions & Answers

insert string at end of line if it found from list

Hi all, can some one help me out file 1 i have 06/01 3:14 d378299 06/01 8:10 d642036 06/01 10:51 d600441 06/01 10:52 d600441 06/01 11:11 d607339 06/01 11:49 d398706 something like this and in file named list i have ( there is space btwn 06/01 and 11:49 and d398706) d607339... (5 Replies)
Discussion started by: zozoo
5 Replies

5. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

6. Shell Programming and Scripting

read a string from its end to its start and stop when you find a specific character

How can I do this? Actually I have a file which contains a path e.g. /home/john/Music/hello.mp3 and I want to take only the filename (hello.mp3) So, I need to read the file from its end to its start till the character "/" Is this possible? Thanks, I am sure you'll not disappoint me here! Oh,... (9 Replies)
Discussion started by: hakermania
9 Replies

7. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

8. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

9. UNIX for Dummies Questions & Answers

How can I insert character to end of file?

Hello all How can I insert character to the end of text file without opening it in vi Just simple one liner, can it be done? Tnx (1 Reply)
Discussion started by: umen
1 Replies

10. UNIX for Advanced & Expert Users

Read text file from a specified string to the end

Hi All, I like to read the log file from specific time to end of the file. eg: message date and time message 1 date and time1 message 2 EOF I want to read all the text (Messages) after date and time to end of the file. Please let me know the UNIX command to perform this?. Thanks,... (9 Replies)
Discussion started by: bsrajirs
9 Replies
Login or Register to Ask a Question