Add sth to end of each line, but only for specific files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add sth to end of each line, but only for specific files
# 1  
Old 07-17-2011
Add sth to end of each line, but only for specific files

Hi.

I have a list with files, and I would like to add a variable to the end of each line of each file in this list. The files are in a folder together with a large number of other files which I don't want to change.

My code is:
Code:
for file in 'cat ../list'
do
    sed 's/$/\/R:_0/' $file >> $file.tmp
    mv $file.tmp $file
done

But of course it doesn't work. Can someone help please?

Cheers,

Kat
# 2  
Old 07-17-2011
The main reason is probably the use of regular single quotes instead of backquotes, but it may also fail for other reasons (file names with spaces or command line length). If there is one file name per line in file.list, try:
Code:
while read file
do
  sed 's/$/\/R:_0/' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
done < ./list

# 3  
Old 07-18-2011
Yes, it was the nasty back quotes! Thanks!

I have a different problem now. I would like to copy the last 5 lines in each file on that list to a textfile.

I wrote this code:

Code:
for i in `cat ../list`
do
    tail -n 5 chp_blizzard_*  $i >> out.data
done

I have around 700 lines in the list, each line contains the name of one file...but the ouput I get is a 3GB textfile with millions of lines because it doesn't only copy the lines from the files on that list but from other files as well.
# 4  
Old 07-19-2011
Quote:
Originally Posted by Bloomy
Yes, it was the nasty back quotes! Thanks!

I have a different problem now. I would like to copy the last 5 lines in each file on that list to a textfile.
Code:
while read i
do
  tail -n 5 "$i" >> out.data
done < ../list

# 5  
Old 07-19-2011
Mh...the files for reading, the script and the list are all in the same folder. Nevertheless, when I execute the script I get the message "tail: cannot open file for reading, no such file or directory. The script looks like this:

Code:
while read i
do
  tail -n 5 "$i" >> ../out.data
done < ./list

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete lines that contain a pattern from specific line to the end.

Gents, I am trying to delete all lines which start with "H" character, but keeping the fist header. Example In the input file I will delete all lines starting from line 8 which contents character "H" to the end of the file. I try sed '8,10000{/^H/d;}' file But as don't know the end... (1 Reply)
Discussion started by: jiam912
1 Replies

2. Shell Programming and Scripting

How to add a line to the end of a set of files without using sed command?

I understand that the SED command reads all the lines in the file before adding a required line to the end of the file. Is there another command that adds a line to the end of files without reading the entire file.... SED is increasing the processing time as the number of lines in each of the... (1 Reply)
Discussion started by: Kanch
1 Replies

3. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

4. Shell Programming and Scripting

help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks, I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers. I was not able to do that with the help of a script. All I could was: for i in server1 server2 server3 server4 do ssh $i done I know 'sed' could be used to... (5 Replies)
Discussion started by: momin
5 Replies

5. UNIX for Dummies Questions & Answers

Merge files and add file name to the end of each line

Hello everybody, I'm trying to merge a lot of files, but I want to include the filename to the end of each line. I've tried to use cat, but I got stuck. My files are for example: file01.001 123456 aaa ddd ee 458741 eee fff ee file02.003 478596 uuu ddd ee 145269 ttt fff ee ... (4 Replies)
Discussion started by: ernesto561
4 Replies

6. Shell Programming and Scripting

awk one line, change \n to sth in a file

Hi Everyone, cat 1.txt aaa bbb ccc outout will be cat 2.txt ,,aaa,,bbb,ccc,, means change "\n" to ",,", and add ",," into the beginging and ending. right now i am using perl while to open and read the file, then split \t, feel not nice. please advice. and i hear using perl... (8 Replies)
Discussion started by: jimmy_y
8 Replies

7. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

8. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

9. Shell Programming and Scripting

check position of end of line for some specific lines

-------------------------------------------------------------------------------- Have to check in a file that the lines starting with 620 and 705 are ending at same posiotin. 82012345 62023232323 70523949558 62023255454 9999 In the above lines, i have to check the lines starting... (1 Reply)
Discussion started by: senthil_is
1 Replies
Login or Register to Ask a Question