Inserting filename at the top of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting filename at the top of file
# 1  
Old 04-09-2013
Inserting filename at the top of file

Hi Forum members,

I want to insert the filename at the top of file (i.e, first row only)

My file name is like
Code:
 asdf_432

And I want to insert this filename in first row as
Code:
filename is asdf with serial no. 432

Thanks in advance. Smilie
# 2  
Old 04-09-2013
Code:
#! /bin/bash

f="asdf_432"
x=${f%_*}
y=${f#*_}

sed -i.bak "1i filename is $x serial no. $y" $f

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 04-09-2013
Try also:
Code:
$ echo filename is ${f%_*} serial no. ${f#*_} | cat - $f

This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-11-2013
Code given above are working well, but what if the filename consists of more than 1 underscore. And I want to apply this condition on last underscore. i.e, my filename is like:
Code:
asdf_01_1234

And I need to print :
Code:
Filename is asdf_01 with serial no. 1234

at the top of the file.

Thanks in advance.
# 5  
Old 04-11-2013
Hey, come on, be reasonable, and be creative. If you want to print "with", embed "with" into the text.
And, for "last underscore", man bash:
Quote:
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern
matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the
shortest matching pattern (the ``#'' case) or the longest matching pattern (the ``##'' case) deleted.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Inserting IDs from a text file into a sequence alignment file

Hi, I have one file with one column and several hundred entries File1: NA1 NA2 NA3And now I need to run a command within a mapping aligner tool to insert these sample names into a sequence alignment file (SAM) such that they look like this @RG ID:Library1 SM:NA1 PL:Illumina ... (7 Replies)
Discussion started by: nans
7 Replies

3. Shell Programming and Scripting

Inserting variable values in filename

Hi All, I have a directory containing multiple files. and also a txt file which contains the list of all filenames and certain values. I want to read the text file line by line and if its 2nd column is matched with the filename in directory, then it must insert the values in 7th column to... (14 Replies)
Discussion started by: CAch
14 Replies

4. HP-UX

Inserting Stinr in between some where in file

Hello Mates, I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G SE18 6RN,,,,5439070,1786840,,1000002148671600,123434 Out put should be: SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434 Can some one help me... (0 Replies)
Discussion started by: krsnadasa
0 Replies

5. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

6. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

7. Shell Programming and Scripting

Inserting variable value into filename

Greetings, people of UNIX/Linux forums. I am having a problem with a script, where I am trying to create a new variable. The value of this variable would be dependent on the value in a couple other previous variables (all variables are 2-digit integers). Here is my code: #set the stations... (3 Replies)
Discussion started by: TheSMan5
3 Replies

8. Shell Programming and Scripting

inserting line to a file

I have posted it previously but somehow could not delete the previous post.I felt i could not explain the problem statement well. Here t goes.I have a file say File1. Now i need a specific pattern from the lines to be added to the other line. File: red blue green ABC.txt@ABC END black... (1 Reply)
Discussion started by: ngupta
1 Replies

9. UNIX for Dummies Questions & Answers

Inserting argument into top of a file

Hi, I am new to Unix, and I am trying to append a line of argument into a current file. I need this line to be inserted into the very top of the file. Does anyone know how this is done? For example, I am trying: echo "insert to top" >> filename. This inserts the line at the bottom of the... (7 Replies)
Discussion started by: Dev06
7 Replies

10. Shell Programming and Scripting

inserting a String in the file(s)

Hi, I'm a newbee to Unix shell scripting. I want to write a shell script that inserts a new String(name&value pair) into a file(s) at a particular place.I willl have to write one script which when executed should insert a new variable in all the files in that particular directory. Say for eg:... (4 Replies)
Discussion started by: 2tbee
4 Replies
Login or Register to Ask a Question