change first line of a file using bash

2024/7/7 6:14:31

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

It looks like:

h\tn0   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 \t 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/(.*)\t(REGION.*)/\1\n\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/(.*)\t(REGION.*)/\1\n\2/;1s/\\t[^[:space:]]+//\"","arg_t1")) 
sed: -e expression #1, char 20: unterminated `s' command
Answer

This isn't a bash job, it's a job for ed or sed. For instance, sed -i -e '1s/\\tn0\s*/\t/' -e '1s/\s*\(hilu\)/\n\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.

https://en.xdnf.cn/q/119874.html

Related Q&A

1 to 2 matching in two dataframes with different sizes in Python/R

please help me with this problem Ive been struggling all day lol, solution in either Python or R is fine! Please help Im really stuck!!! I have two dataframes - df1 has 44 rows, df2 has 100 rows, they …

Can someone fix this? pip install intents

Im not sure why this is failing but I need help to fix it please Ive been trying to fix it for about 2 hours now and cant figure it out. I have tried to restart my computer, look it up on google etc. b…

Calculating Average Performance Measures in M/M/1 System Using SimPy

I am seeking to calculate the average waiting times, and average service times in the following M/M/1 queueing system, but I am not able to calculate the averages. It writes down to the console each cu…

Python: Whats the difference between import X and from X import *? [duplicate]

This question already has answers here:Use import module or from module import?(23 answers)Closed 7 years ago.I use to think both are equal until I tried this:$python Python 2.7.13 (default, Dec 17 20…

Python iterate through pixels of image

Im trying to iterate through pixels of an image. I set the size and then use a for loop, however I get a type error: object not iterable. I have imported PIL and Imagew=100 h=200 im=im.resize((w,h), Im…

Geocoding with Geopy and big data

I have this CSV file which im feeding with this python scriptimport csv from geopy.geocoders import OpenCagegeolocator = OpenCage() #here some parameters are needed with open(/Users/Ian/Desktop/Test02/…

an error in sending json data to flask server [duplicate]

This question already has answers here:How to get POSTed JSON in Flask?(13 answers)Closed 1 year ago.I have a json data as {"age":59.0,"bp":70.0,"sg":1.01,"al":…

terminate a python program when it hanged using subprocess python

I have a main.py which open a new cmd (subprocess) when another program (test.py, in same directory) is hanged. To determining test.py is hanged or not, I used latest modified time (os.path.getmtime(te…

causes of Python IOError: [Errno 13] Permission denied

When attempting to write a file, I can get this same error when any of following conditions applies:The file exists and is marked read-only. I dont have write permission for the folder and therefore ca…

Python Invalid Syntax IF statement

Im trying to make a quiz in python but I keep getting invalid syntax errors.#This is for addition questions.if (question=add) <---- That is where i get the error for i in range(0,10):first_number_a…