Prepending lines with file name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Prepending lines with file name
# 1  
Old 08-07-2011
Prepending lines with file name

I have a number of dat-files, such as abc.dat, def.dat etc, as follows:
Code:
2011-07-01  100.0
2011-07-02  101.0
2011-07-03  101.7

I want to prepend the file with the base file name; so for abc.dat it would result in the following:
Code:
abc  2011-07-01  100.0
abc  2011-07-02  101.0
abc  2011-07-03  101.7

How do I achieve that? The closest I currently have is the following:
Code:
for file in *.dat; do
  $prepend=`echo $file | cut -d "." -f 1`
  sed -i '' -e 's/^/$prepend /' $file
done

Also, how can the same command be used to delimit by tab (\t) instead of space?
# 2  
Old 08-07-2011
It can be done in single line by Perl:
Code:
perl -i -pe '$ARGV=~s/\..*//;$_="$ARGV\t$_"' *.dat

# 3  
Old 08-07-2011
Thank you for your response. And you may already have seen that the title of the post should read: "Prepending lines with file name"
Code works nicely by the way. Nonetheless, can you also achieve the same in the shell script?
# 4  
Old 08-07-2011
It will work all the same if you put it into shell script like this:
Code:
#!/bin/bash
perl -i -pe '$ARGV=~s/\..*//;$_="$ARGV\t$_"' *.dat

# 5  
Old 08-07-2011
Ok, thanks again.
# 6  
Old 08-07-2011
bash only
Code:
for file in *.dat;do while read line;do echo -e "${file/.dat/}"\\t"$line";done < "$file";done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

2. Shell Programming and Scripting

Sed script for appending & prepending

Hello Mates I am trying to write a script, which appends and prepends the text in a file. I tried testing with a small file and it worked fine. but for the large file, the script is wiping the entire file and adds only the word to be appended in the file. mv $file_name $file_name.bak sed... (6 Replies)
Discussion started by: sathyaac
6 Replies

3. Shell Programming and Scripting

[Shell/Perl(?)] Prepending timestamps to console output & writing results to a file

I do a lot of TSM work and I embarked on what I thought would be an easy task, and I'd be very happy for any input to save the pounding my keyboard is receiving :] By default, the output of TSM's console has no timestamping, making it hard to sort through accurately. This puts my console into... (5 Replies)
Discussion started by: Vryali
5 Replies

4. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

5. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

6. UNIX for Dummies Questions & Answers

Prepending information from filenames into files

I would like to know how to take information from a filename and place it into the text of the same file. Let's say I have a file called height_2_width_1.txt containing data that is related to a height of 2 and a width of 1, and the text originally looks like this: where these two columns... (13 Replies)
Discussion started by: Scatterbrain26
13 Replies

7. Shell Programming and Scripting

Redirection after prepending timestamp

Hi all, I have little experience with Scripting so hoping someone may be able to help me or point me in the right direction. I have a shell script which was outputting uncaught exceptions to a log file. $JAVA_MAIN_CLASS > $LOGNAME 2>&1 What I want to be able to do is prepend a timestamp on... (8 Replies)
Discussion started by: MacAonghusa
8 Replies

8. Shell Programming and Scripting

Conditionally prepending text

I am currently writing a script to compare a file list created over an FTP connection to a local directory. I have cleaned the FTP file list up so that I just have a raw list of filenames however due to the directory structure employed (both locally and on the ftp site) I need to prepend each line... (6 Replies)
Discussion started by: Dal
6 Replies

9. Shell Programming and Scripting

Extra/parse lines from a file between unque lines through the file

I need help to parse a file where there are many records, all of which are consistently separated by lines containing ^============= and "^ End of Report". Example: ============= 1 2 3 4 End of record ============= 1 3 4 End of record Etc.... I only need specific lines... (5 Replies)
Discussion started by: jouuu
5 Replies

10. Shell Programming and Scripting

Finding pattern & prepending a line with text

Hello Dudes, I have a task to make a unix shell script that should search for a specific TEXT in a file.If that TEXT is found, shell script should add a comment statement before that TEXT line. Ex : LINE 1 xxxxx LINE 2 xxxx CALL xxxx LINE 3 xxxx PERFORM UNTIL if i... (1 Reply)
Discussion started by: kirrushna
1 Replies
Login or Register to Ask a Question