Linked Questions
21 questions linked to/from How to change a file in-place using awk? (as with "sed -i")
29 votes
4 answers
2k views
How to safely use gawk's -i option or @include directive?
Using gawk -i inplace some-awk-code some-file (or @include "inplace" within an awk script) to edit a file in-place (or any other extension) is a security vulnerability. Why? How do I work ...
7 votes
1 answer
33k views
How to replace the first line using sed?
Hi i am trying to replace the header line from my file using the sed command as mentioned below, sed "1s/.*/$new_header/" Test_file.csv The above line replaces the header and prints in stdout , But ...
3 votes
3 answers
6k views
How to delete lines containing string in column n using awk?
I have a tab-separated file that looks like this: $ cat in_file NC_013132.1 7260299 7261429 WP_012793281.1 NC_013132.1 7270674 7270862 NC_013132.1 7270674 7270862 ID=cds5678 NC_013132.1 ...
0 votes
4 answers
2k views
How to correctly find a line number with awk and delete it with sed in bash?
I want to delete a specific line of a file in bash. What I am currently doing is to get the line number and pass it so sed to delete this line: awk '/qr/{ print NR; exit }' test | sed -i "${1}d" ...
0 votes
1 answer
5k views
How to add columns to a file (preferably using sed -i) when there is only one column?
Say I have a column of data like this: sample123 sample456 samplexyz I can easily add two new columns using: sed -i "s/$/\t1.1\tC/" <file.txt> Now it's: sample123 1.1 C sample456 1.1 C ...
1 vote
4 answers
933 views
comment the specific line in fstab file that contained the relevant UUID number
We want to comment the specific line in fstab file that contained the relevant UUID number Example: Disk=sde UUID_STRING=` blkid | grep $Disk | awk '{print $2}' ` echo $UUID_STRING UUID="...
0 votes
5 answers
652 views
How to replace an empty line with the line below it?
I've parsed a file but there's some issue and a quick way around is to replace empty line with line below it so how do I do that like the folowing file apple banana big sig cake should convert to ...
0 votes
3 answers
1k views
Remove https characters in a text file
I need to remove all http and https from txt file. Like this: http://ac.tecnicasdeinvasao.com http://go.tecnicasdeinvasao.com http://lp.tecnicasdeinvasao.com https://ac.tecnicasdeinvasao.com http://...
0 votes
4 answers
2k views
how do I remove everything in the column after the 2nd colon
I have a file with about 7 million lines which looks like this: head gokind_SNPs.txt 1:753541:G:A 1:769223:C:G 1:771967:G:A 1:778745:A:G 1:779322:A:G ... How do I remove everything after the 2nd ...
3 votes
3 answers
2k views
Delete duplicate lines in file without creating new file in ubuntu
I can't seem to find a command that lets me delete duplicates in my file without creating a new file and also preserving the order of the contents in my file. Would there be another command besides ...
0 votes
2 answers
1k views
Substitute, Print and Delete lines in file
I'm using sed to do the following: Match pattern (eg: line starting with #) Substitute match (eg: substitute the # char at the start to nothing/remove it) Print to stdout (eg: print the resulting ...
0 votes
2 answers
1k views
Delete tab-delimited columns matching sub-string in first line
I would like to delete all tab-delimited columns from a text file in which the header (first line) contains the string "_HET". The input text file looks like this: rs36810213_HET rs2438689 ...
1 vote
5 answers
238 views
Copy digits from end of string to another
How could I copy digits from one end of a string to another end of a string? So example, Input - Example123:Hello Exp12:Hey1 Exp:heylo expected output - Example123:Hello123 Exp12:Hey112 Exp:heylo I'...
0 votes
3 answers
1k views
Remove the last rows with no data but just commas from a CSV file through shell script
I am generating a CSV file which ideally should have 1-row or 2-row data but it is coming like 11,ABC,3,4,5,6 ,,,,, I have tried sed and awk commands to remove that empty row but it didn't work out ...
0 votes
1 answer
988 views
Insert characters at the middle and the end of a line with specific line length
Currently I am working on a folder of files, and each file have some lines as follows: abcde fghij abcde fghij jklmn pqrst ..... ..... These lines have a specific line length 43. At the ...