Append variable texts to the beginning of each line in all files in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append variable texts to the beginning of each line in all files in a directory
# 1  
Old 03-15-2012
Append variable texts to the beginning of each line in all files in a directory

I am writing a code to append some numbers in the beginning of each line in all the files present in a directory. The number of files are really huge. The files are numbered as 1.sco, 2.sco, 4.sco (Note: 3.sco is missing).
The files currently look like this:

1.sco
Code:
2 3
5 6
6 7

My task is to open each 1.sco, 2.sco and so on and add a number based on its file name at the beginning of each line in all files. This is what I intend, sample output:

1.sco
Code:
1 2 3
1 5 6
1 6 7

2.sco
Code:
2 2 3
2 1 5
2 6 7
2 3 5
2 7 8

I have written a code but not getting where actually the problem is?

Code:
for num in `seq 1 300000`; do
 test ! -f $num.sco && continue
 echo '%s/^/$num /g\nwq!' | ex - $num.sco
done

The first line is a loop which generates the numbers. Then second lines tests for the presence of file and third line adds a text in the beginning of each line. I am using Linux with BASH.
# 2  
Old 03-15-2012
Is it the filename (without its extension) that you want to put in front of each line? If so, then try this:

Code:
for file in *.sco; do sed -i "s/^/$(basename $file .sco) /g" $file; done

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 03-15-2012
Bash

Hi,

I think you are trying to do something like this,

Code:
#! /bin/bash
cd "/home/path"
for i in *.sco
do
  s=${i%.sco}
  if [ -f $i ];then
    sed "s/^/$s /" $i >tmp
    mv tmp $i
  fi
done

Cheers,
RangaSmilie

Last edited by rangarasan; 03-15-2012 at 03:24 AM..
This User Gave Thanks to rangarasan For This Post:
# 4  
Old 03-15-2012
Code:
for i in *.sco; do  awk -v A=`basename $i .sco`  '{print A,$0}' $i; done

This User Gave Thanks to ningy For This Post:
# 5  
Old 03-15-2012
awk

Quote:
Originally Posted by ningy
Code:
for i in *.sco; do  awk -v A=`basename $i .sco`  '{print A,$0}' $i; done

You need to save the informaion in the same file then you can use System Variable FILENAME in awk.

Code:
for i in *.sco; do awk -v A=`basename $i .sco` '{a[NR]=$0;}END{for(i=1;i<=NR;i++){print A,a[i] >FILENAME;}}' $i; done

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

2. Shell Programming and Scripting

How to append in the beginning not at the end?

Hi, I now that >> will append text to the end of the text that is already inside the file. How to append the new text infront of the text that is already in the file. Thanks for any input. Regards, Chandu (3 Replies)
Discussion started by: chandrakanth
3 Replies

3. Shell Programming and Scripting

How to Append data to the content at the beginning of a variable?

Hi Guys, IF i have to append data to a variable, i simply use the following. What can i do to append data to the beginning? VAR+="data" (6 Replies)
Discussion started by: srkmish
6 Replies

4. Shell Programming and Scripting

How to pre-append a variable string to each line in a file?

How to pre-append a variable string to each line in a file contains both single and double quotes? The variable string has no quotes in it. thank you very much. :confused: (8 Replies)
Discussion started by: dtdt
8 Replies

5. Shell Programming and Scripting

Append Multiple files with file name in the beginning of line

Hi, I have multiple files having many lines like as bvelow: file Name a.txt abc def def xyz 123 5678 file Name b.txt abc def def xyz 123 5678 I would like to append files in the below format to a new file: file Name c.txt (7 Replies)
Discussion started by: rramkrishnas
7 Replies

6. UNIX for Dummies Questions & Answers

sed - Add a variable line to the end of a block beginning with a regex

Hi, Need some help with sed. I have a file that has sections : e.g. a=blah b=blah d=blah e=blah There's many sections in the file. (1 Reply)
Discussion started by: andyatit
1 Replies

7. Shell Programming and Scripting

Append file name to the beginning of each line

I want to append file names at the beginning of a line for each row file content abc.txt.gz 123|654|987 bcd.txt.gz 876|trf|kjh I want a single output file with below format abc.txt.gz|123|654|987 bcd.txt.gz|876|trf|kjh This one is working but only with unzip files,need to have... (3 Replies)
Discussion started by: rakesh5300
3 Replies

8. UNIX for Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 Replies

9. Shell Programming and Scripting

search directory-find files-append at end of line

Hi, I have a command "get_data" with some parameters in few *.text files of a directory. I want to first find those files that contain this command and then append the following parameter to the end of the command. example of an entry in the file :- get_data -x -m50 /etc/web/getid this... (1 Reply)
Discussion started by: PrasannaKS
1 Replies

10. Shell Programming and Scripting

input a line at the beginning of every file in a directory?

if need to input a word or anything at the beginning of every file in a directory. how do i accomplish this? say the file is named hyperten. how do i make hyperten the first line of every file in a given directory? thanks (6 Replies)
Discussion started by: Terrible
6 Replies
Login or Register to Ask a Question