Sponsored Content
Top Forums Shell Programming and Scripting How to prepend filename to its content without a third file? Post 302972769 by yifangt on Tuesday 10th of May 2016 03:52:32 PM
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
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
PDF::API2::Page(3pm)					User Contributed Perl Documentation				      PDF::API2::Page(3pm)

NAME
PDF::API2::Page - page management METHODS
$page = PDF::API2::Page->new $pdf, $parent, $index Returns a page object (called from $pdf->page). $page = PDF::API2::Page->coerce $pdf, $pdfpage Returns a page object converted from $pdfpage (called from $pdf->openpage). $page->update Marks a page to be updated (by $pdf->update). $page->mediabox $w, $h $page->mediabox $llx, $lly, $urx, $ury $page->mediabox $alias Sets the mediabox. This method supports the following aliases: '4A', '2A', 'A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', '4B', '2B', 'B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'LETTER', 'BROADSHEET', 'LEDGER', 'TABLOID', 'LEGAL', 'EXECUTIVE', and '36X36'. ($llx, $lly, $urx, $ury) = $page->get_mediabox Gets the mediabox based one best estimates or the default. $page->cropbox $w, $h $page->cropbox $llx, $lly, $urx, $ury $page->cropbox $alias Sets the cropbox. This method supports the same aliases as mediabox. ($llx, $lly, $urx, $ury) = $page->get_cropbox Gets the cropbox based one best estimates or the default. $page->bleedbox $w, $h $page->bleedbox $llx, $lly, $urx, $ury $page->bleedbox $alias Sets the bleedbox. This method supports the same aliases as mediabox. ($llx, $lly, $urx, $ury) = $page->get_bleedbox Gets the bleedbox based one best estimates or the default. $page->trimbox $w, $h $page->trimbox $llx, $lly, $urx, $ury Sets the trimbox. This method supports the same aliases as mediabox. ($llx, $lly, $urx, $ury) = $page->get_trimbox Gets the trimbox based one best estimates or the default. $page->artbox $w, $h $page->artbox $llx, $lly, $urx, $ury $page->artbox $alias Sets the artbox. This method supports the same aliases as mediabox. ($llx, $lly, $urx, $ury) = $page->get_artbox Gets the artbox based one best estimates or the default. $page->rotate $deg Rotates the page by the given degrees, which must be a multiple of 90. (This allows you to auto-rotate to landscape without changing the mediabox!) $gfx = $page->gfx $prepend Returns a graphics content object. If $prepend is true the content will be prepended to the page description. $txt = $page->text $prepend Returns a text content object. If $prepend is true the content will be prepended to the page description. $ant = $page->annotation Returns a new annotation object. $page->resource $type, $key, $obj Adds a resource to the page-inheritance tree. Example: $co->resource('Font',$fontkey,$fontobj); $co->resource('XObject',$imagekey,$imageobj); $co->resource('Shading',$shadekey,$shadeobj); $co->resource('ColorSpace',$spacekey,$speceobj); Note: You only have to add the required resources, if they are NOT handled by the *font*, *image*, *shade* or *space* methods. AUTHOR
Alfred Reibenschuh perl v5.14.2 2014-03-09 PDF::API2::Page(3pm)
All times are GMT -4. The time now is 08:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy