Replacing first line of file by >filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing first line of file by >filename
# 1  
Old 04-07-2011
Lightbulb Replacing first line of file by >filename

Hi All,
I have a set of files named
Code:
S5_SK1.chr01 S5_SK1.chr02 S5_SK1.chr03 .....

and the first line of these files is
Code:
>SK1.chr01 >SK1.chr02 >SK1.chr03

..... Can anyone suggest how I can change the first line of all these files with the filename itself? So my expected output for the first lines of the files would be
Code:
>S5_SK1.chr01 >S5_SK1.chr02 >S5_SK1.chr03 .....

somone help me get around this ?
Cheers and have a nice daySmilie
# 2  
Old 04-07-2011
Each individual file should have the names of all the files? Smilie
# 3  
Old 04-08-2011
No No ... for example
file called S5_SK1.chr01
has first line >SK1.chr01, but
I would like the first line of this file to be >S5_SK1.chr01

and so on for the rest of the files

Cheers Smilie

---------- Post updated 04-08-11 at 02:58 AM ---------- Previous update was 04-07-11 at 11:10 AM ----------

Just to clarify the above question:

Hi All,
I have a set of files named
Code:
S5_SK1.chr01
S5_SK1.chr02
S5_SK1.chr03
.....
and the first line of these files is
Code:
>SK1.chr01
>SK1.chr02
>SK1.chr03
.....


..... Can anyone suggest how I can change the first line of all these files with the filename itself? So my expected output for the first lines of the files would be
Code:
>S5_SK1.chr01
>S5_SK1.chr02
>S5_SK1.chr03
.....Hope that made it look clearer ...

I'll appreciate the feedback

Cheers and good day Smilie
# 4  
Old 04-08-2011
Code:
awk '{if( NR==1)print ">"FILENAME;else print}' S5_SK1.chr01

This User Gave Thanks to tene For This Post:
# 5  
Old 04-08-2011
Try:
Code:
perl -i -pe 's/.*/>$ARGV/ if $.==1' S5_*

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 04-08-2011
Code:
FILE_PATH="<write the path here>"
FILE1="${FILE_PATH}/S5_SK1.chr01"
FILE2="${FILE_PATH}/S5_SK1.chr02"
FILE3="${FILE_PATH}/S5_SK1.chr03"
BASENAME1=`basename ${FILE1}`
BASENAME2=`basename ${FILE2}`
BASENAME3=`basename ${FILE3}`

FIRST_LINE1=`cat ${FILE1} | sed q`
FIRST_LINE2=`cat ${FILE2} | sed q`
FIRST_LINE3=`cat ${FILE3} | sed q`

cat ${FILE1} | sed "s/${FIRST_LINE1}/${BASENAME1}/" > ${FILE1}.temp
mv ${FILE1}.temp ${FILE1}

cat ${FILE2} | sed "s/${FIRST_LINE2}/${BASENAME2}/" > ${FILE2}.temp
mv ${FILE2}.temp ${FILE2}

cat ${FILE3} | sed "s/${FIRST_LINE3}/${BASENAME3}/" > ${FILE3}.temp
mv ${FILE3}.temp ${FILE3}

# 7  
Old 04-08-2011
Thanks Tene and Bartus, but with both these methods I have to run the code to append each single file .... How can I automatise it for the whole lot of files at once ?
Cheers Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replacing part of filename

Hi guys! I have quite a lot of files like all_10001_ct1212307460308.alf* and I want to get rid of the first number for all at once like: all_ct1212307460308.alf* How can I do this in the shell? (12 Replies)
Discussion started by: TimmyTiz
12 Replies

2. Shell Programming and Scripting

Replacing a line in a file

Hi all, I need to replace a line export TZ=xxxxxxxx with the line export TZ=$1 Now, "xxxxxxxx" in the above line is some unknown string and $1 is a parameter. I want the content of $1 to be replaced with "xxxxxxxx". Kindly help me how to do this in the shell scripting. (5 Replies)
Discussion started by: ddeeps2610
5 Replies

3. Shell Programming and Scripting

Replacing a line in a file using sed

I have a file which has a list in it pop triangle people slow fast What I want to do is search this list and replace people with humans do the list looks like this: pop triangle human slow fast I think i use something like this.... if cat /list.txt | grep -q 'people' ; then (9 Replies)
Discussion started by: digitalviking
9 Replies

4. Shell Programming and Scripting

Replacing line 'i' of file1 with line 'j' of file 2

Hi All, As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2 e.g. file 1 wqwert 4.4464002 3 319 286 369 46.320002 56.150002 45.100002 1 1 1 0.723 (12 Replies)
Discussion started by: f_o_555
12 Replies

5. Shell Programming and Scripting

Adding filename to each line of the file

Hi, I am a relative new bee in scripting. I need to develop a script such that the code would iterate through each file in a source directory and append every line of the file with '|' and the corresponding file filename. eg INPUT file IF927_1.dat - H|abc... (4 Replies)
Discussion started by: scripting_newbe
4 Replies

6. UNIX for Dummies Questions & Answers

Extract first line of a file and use as filename

I am trying to find a way to create a script which will extract the first line of a file and then rename the file (or create a new file with the same content as the old file) using the first line as the name. The first line being a single word, that is. I am hopeless at programming, if anyone can... (5 Replies)
Discussion started by: s.plumb
5 Replies

7. Shell Programming and Scripting

Replacing the last character for each line in a file

Hello, I have a csv file and will like to replace the last character of each line in the file with Z (20 Replies)
Discussion started by: 123script
20 Replies

8. UNIX for Dummies Questions & Answers

Replacing string with filename

Hi All, I've recently run a script that inserts the filename into all files of my active directory. Now I want to move the filename string and have it replace text a few lines down. In other words, here's what I'm trying to do. Here is a file called 'goodtimes': " goodtimes Hi, Welcome... (1 Reply)
Discussion started by: calrog
1 Replies

9. Shell Programming and Scripting

Replacing a line in a file - HELP!!!

I have a problem in the following code ... while read line do #Get Line Number OLDLINE=`sed -n $Lineno $filename` echo "Un Changed Line : "$OLDLINE echo "Enter a New Pattern : " read NewPattern <&1 echo "NewPattern :"$NewPattern NEWLINE=`cat $filename | sed -n... (1 Reply)
Discussion started by: maxmave
1 Replies

10. UNIX for Advanced & Expert Users

replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file? see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can,... (3 Replies)
Discussion started by: Terrible
3 Replies
Login or Register to Ask a Question

Featured Tech Videos