Edit first line of a text file

Hi friends,

Issue1:

I have a text file with the first line like this

#chrom	start	end	Readcount_A	Normalized_Readcount_A	ReadcountB	Normalized_Readcount_B	Fc_A_vs_B	pvalue_A_vs_B	FDR_A_vs_B	Fc_B_vs_A	pvalue_B_vs_A	FDR_B_vs_A	<a href="http://unix.com/">Link</a>

How can I change it to the following without opening the file?

#chrom	start	end	Readcount_KO	Normalized_Readcount_KO	Readcount_WT	Normalized_Readcount_WT	Fc_KO_vs_WT	pvalue_KO_vs_WT	FDR_KO_vs_WT	Fc_WT_vs_KO	pvalue_WT_vs_KO	FDR_WT_vs_KO	Link

Issue2:

How do I add the below line to a text file as a first line?

#chrom	start	end	Readcount_KO	Normalized_Readcount_KO	Readcount_WT	Normalized_Readcount_WT	Fc_KO_vs_WT	pvalue_KO_vs_WT	FDR_KO_vs_WT	Fc_WT_vs_KO	pvalue_WT_vs_KO	FDR_WT_vs_KO	Link

I am looking for something in awk. Thanks in advance.

Why awk? What have you tried so far?

You always have to open a file, wether for reading, writing or both, afaik. Maybe you mean something else instead of "open".

Issue 2:
Write the line to a file and add the long file to the one-liner file by shell's redirection/concatenation.

Hello,
You can try this:

with ">>" append this to the file and whit ">" remove the content of the file.

1 Like

use sed to replace without opening file .

Nothing can edit a file without opening it. Files don't work that way.

 
sed 's/A/KO/g' > corona688.txt

it will change the text without opening the file.

or you can use

 
sed -i 's/A/KO/g' 

this will save in the current file.

1 Like

I see. Something else had to open it in the first place, however, if not reading from stdin.

It does NOT save it in the current file. It deletes the file and creates a new one. This can have bad effects on file ownership and permissions.

1 Like

but this is what smith requires, he wants to change it without opening ,

He probably didn't want to replace it either, then.

You cannot edit a file without opening it. Something, somewhere has to open it.

1 Like

When I meant not opening, I meant about opening manually doing a nano or a vi and replacing stuff. Sorry for any confusion.

As others have pointed out, you cannot edit a file without opening the file. If all you want to do is replace or insert or insert a single line, the simplest and quickest way would be to do it the old way and use ed . If it needs scripting, just put the ed commands in a here document. If you cannot work ed there has to be a problem.

Comments:

Not clear why this is posted in scripting when only two files are mentioned.

I too cannot see why one would want to use awk for basic text editing.

Be wary of the naive sed substitute suggestion because could change lines lines other than the first line. A sed swapping the complete line is safer providing that it is the only line in the file which matches.

1 Like

Thanks methyl. I was learning awk since a little while. So, wanted to know how to do it. I knew sed is lot more easier for basic text editing. But, wanted to see how awk does it. Your time is appreciated.

1 Like