How to prepend filename to its content without a third file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to prepend filename to its content without a third file?
# 1  
Old 05-10-2016
How to prepend filename to its content without a third file?

Hello,
Is possibly there a way to prepend the filename to its content without a third file? The reason is to add a header to each file contents to distinguish each other when they are pasted side-by-side.
Code:
sample.txt:

XLOC_001   0
XLOC_002   23
XLOC_003   4
XLOC_012   6

output (with the same name sample.txt):
Code:
sample.txt:

geneID     sample.txt
XLOC_001   0
XLOC_002   23
XLOC_003   4
XLOC_012   6

So far, I've tried:
Code:
for i in *.txt; do (echo "geneID    $i" ; cat $i) > ${i}; done

but got error: input file is output file
I'm aware of the solution is to use a tmp file, and rename it to the original name.
Code:
for i in *.txt; do (echo "geneID    $i" ; cat $i) > ${i}.tmp; mv ${i}.tmp ${i}; done

I am wondering if there is a way to do this job without the "tmp" file. The closest function I can think of is to add filename to its content within vim and :wq which does not require a third file name, but I have hundreds of files to modify.
Thanks!

Last edited by yifangt; 05-10-2016 at 05:28 PM.. Reason: typo
# 2  
Old 05-10-2016
Use ed:

Code:
for file in *.txt
do
     printf "%s\n" 1i "geneID  $file" . w | ed -s "$file"
done

This User Gave Thanks to xbin For This Post:
# 3  
Old 05-10-2016
Thanks xbin!
This worked out to be exactly what I was looking for.
Could you please elaborate the first part of the pipe?
Code:
printf "%s\n" 1i "geneID  $file" . w

I wish vim could be piped to the command line, and I am not familiar with ed at all.
Thanks again!

Last edited by yifangt; 05-10-2016 at 08:28 PM..
# 4  
Old 05-10-2016
If you know vim, you know a lot more about ed than you think you do.

If you're in vim and you type in the command:
Code:
:1i

and then type in the two lines:
Code:
geneID  filename
.

What will happen. (It will insert the text geneID filename as a new line of text that will appear before the 1st line that was in the file before.)

And then what happens if you type in the command:
Code:
:w

(It will write the updated buffer back to the file you are editing.)

The ed (and ex) utilities give you most of the commands that you type into vim that start with a colon, but do not need (or accept) the colon.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-11-2016
Thanks Don!
Now I understand xbin's way using the old editor ed.
The left side of the pipe bugs me with a mixture of printf "%s\n" and other options for ed, as I can only understand this way:
Code:
printf "%b\n" "1i\ngeneID\t$file\n.\nw" | ed -s $file

which seems working too.
When I learned vim, sometime I wished there is similar function as piped to receive stdout.

EDIT: I am so naive! Experts have dug way deeper than what I thought, like my this question. ed, ex, and new vipe in moreutils package.

Thanks again!

Last edited by yifangt; 05-11-2016 at 01:16 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

prepend timestamp to continiously updating log file

Hi, I have a process which outputs to a log. Below is the code snippet: process &> $LOGFILE& The log file keeps on updating whenever a transaction is processed. The log file has a time stamp added so every time I kill the process and start the process a new log file is... (4 Replies)
Discussion started by: rajkumarme_1
4 Replies

2. Shell Programming and Scripting

Prepend TimeStamp to STDERR & STDOUT to a file

Currently I am redirecting STDERR and STDOUT to a log file by doing the following { My KSH script contents } 2>&1 | $DEBUGLOG Problem is the STDERR & STDOUT do not have any date/time associated. I want this to be something that i can embed into a script opposed to an argument I use... (4 Replies)
Discussion started by: nitrobass24
4 Replies

3. Shell Programming and Scripting

Search for string in filename, not file content

How can I search for a string in a filename? For example, I want to know if a filename (not the file contents) contains the string "test". (3 Replies)
Discussion started by: daflore
3 Replies

4. Shell Programming and Scripting

Append and Prepend Text to a file list

I want to print out a directory listing, then append ] to the end of each line. I'm trying to create a list of Wiki links based on folder listings that i can just copy and paste without having to edit 100's of file listings. Using sed i've figured out to do something like this: sed... (4 Replies)
Discussion started by: CapnDoody
4 Replies

5. UNIX for Dummies Questions & Answers

prepend columns to a file

I have two files. File 'a' has contents: 1|1 2|2 3|3 4|4 and file 'b' has contents: abc|def hij|klm nop|qrs tuv|wxy I would like to prepend file 'a' to file 'b' (with pipe) such that the contents of 'b' will be: 1|1|abc|def 2|2|hij|klm 3|3|nop|qrs 4|4|tuv|wxy (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

6. Shell Programming and Scripting

Prepend content of a file to another file

How can i prepend the content of a file to another file? I tried: sed '1r textfile' myfile but it inserted the text AFTER the first line? What would be a good method? I can easily append lines to a file using the ">>" operator, is the something similar in bash to prepend lines? Kind regards... (2 Replies)
Discussion started by: Christoph Spohr
2 Replies

7. Shell Programming and Scripting

find and replace in subdirectory (filename & content - both)

Hi, I have to rename all occurance of CUST_MST to RESELLER_MST both in filename and file content under a directory (say D0) which contains multiple (2-3 levels) sub directory. Example: D0 -> D1 -> D2 has a file CUST_MST_TEMP.txt this contains : > cat /D0/D1/D2/CUST_MST_TEMP.txt... (3 Replies)
Discussion started by: sabyasm
3 Replies

8. Shell Programming and Scripting

Adding filename into file content

Dear Experts, Please help to teach me how to add the filename into the file content. Actually the file name are EVENTS-20050912. ***************New output that I want*************** EVENTS-20050912 03:33:37 ALARM: BTSSPAN-277-1 30-18013 EVENTS-20050912 12:10:28 ALARM: BTSSPAN-297-2... (1 Reply)
Discussion started by: missutoomuch
1 Replies

9. UNIX for Dummies Questions & Answers

find filename based on file content

:confused: There is a flat file on my system which contains email addreses of people in my company. This file is utilized when sending notifications for various things. However nobody knows where this file is located or what it is named. The only thing we know is the email address of a user who... (4 Replies)
Discussion started by: kollerj
4 Replies
Login or Register to Ask a Question