Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

python - change first line of a file using bash

I need to change a single line in a file. It is always in the first line of the file.

It looks like:

h	n0   n1  n2  n3  n4  n5  n6  n7  n8  n9  hilu    cjt 1   1000000

there is a tab in all gaps except after the h.

I would need to re-transform the line into

h  n1   n2  n3  n4  n5  n6  n7  n8  n9  
hilu    cjt 1   1000000

at the beginning o the line the thing and n0 needs to go and there needs to be a tab between h and n1. Then a newline needs to start before hilu but there should be no additional tab after n9

Ideally I would just feed my file to the script and it would not require writing an intermediate script to fill.

is there maybe an efficient version in Perl or python or so? I thought about R but then there are 1000 of lines in the file and only the first lien needs be changed...

tried to use the solution from jahid to run it from r with

> system(paste("sed -r '1s/(.*)	(REGION.*)/1
2/;1s/\t[^[:space:]]+//'","arg_t1")) 
sed: -e expression #1, char 20: unterminated `s' command

with the suggest from the comm I get

> system(paste("sed -r "1s/(.*)	(REGION.*)/1
2/;1s/\t[^[:space:]]+//"","arg_t1")) 
sed: -e expression #1, char 20: unterminated `s' command
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This isn't a bash job, it's a job for ed or sed. For instance, sed -i -e '1s/\tn0s*/ /' -e '1s/s*(hilu)/ 1/' filename can do this. As Perl's foundation is a merging of shell, awk and sed, it can also be used similarly.

The editing itself isn't efficient because POSIX file semantics do not permit inserting or removing data, only (over)writing or truncating. This command therefore copies the file, with only the beginning altered. If done as part of a pipeline (just remove -i to output to stdout) it's practically zero cost. Also, with thousands of lines of data that's still pretty small by today's standards.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...