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
# 8  
Old 04-08-2011
If you have few files starting with "S5_" in your current directory, and you run this code just once: perl -i -pe 's/.*/>$ARGV/ if $.==1' S5_*, then it will automatically change contents of all the "S5_..." files.
This User Gave Thanks to bartus11 For This Post:
# 9  
Old 04-08-2011
Try mine. Not a one-liner though.
# 10  
Old 04-08-2011
Replace file name with *, so it will apply the awk to all the files in the dir.
Code:
awk '{if( NR==1)print ">"FILENAME;else print}' *

# 11  
Old 04-08-2011
OK I can do that ... guess its easier ... Cheers Bartus Smilie
Hv a nice day
PS :Any ideas about my post https://www.unix.com/302511664-post1.html ??
Smilie

---------- Post updated at 04:25 AM ---------- Previous update was at 03:55 AM ----------

Sorry Tene and Bartus but both solutions are only appending the first file first line
Eg I give u head -1 S5_* output after running code
Code:
==> S5_SK1.chr01 <==
>S5_SK1.chr01

==> S5_SK1.chr02 <==
>SK1.chr02

==> S5_SK1.chr03 <==
>SK1.chr03

==> S5_SK1.chr04 <==
>SK1.chr04

==> S5_SK1.chr05 <==
>SK1.chr05

I get the same output using either awk or Perl codes ... How can I resolve this?
Cheers Smilie
# 12  
Old 04-08-2011
Code:
for i in S5_*; do
perl -i -pe 's/.*/>$ARGV/ if $.==1' $i
done

This User Gave Thanks to bartus11 For This Post:
# 13  
Old 04-08-2011
That did the job .... cheers Bartus Smilie
# 14  
Old 04-08-2011
It should have been

Code:
awk '{if( FNR==1)print ">"FILENAME;else print}' *

 
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