This question already has answers here:
Remove the last line from a file in Bash
(16 answers)
Closed 5 years ago.
How can I strip the last line off a text file with Unix tools? Action can take place in place, so probably a version of truncate would be appreciated.
I guess there should be a solution using grep, sed, or head or similar tools, but I couldn't figure out a concise version.
Input:
one
two
three
Output:
one
two
sed is your friend. This prints everything but not the last line of a given file.
$ sed \$d file
Related
This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I am trying to paste two files together
file ip.txt
10.32.216.15
10.23.134.8
10.33.2.37
10.33.84.20
10.33.17.38
file obj.txt
obj-10.32.216.15
obj-10.23.134.8
obj-10.33.2.37
obj-10.33.84.20
obj-10.33.17.38
and I use the command like this:
paste ip.txt obj.txt
However I get this truncated output:
10.32.21obj-10.32.216.15
10.23.13obj-10.23.134.8
10.33.2.obj-10.33.2.37
10.33.84obj-10.33.84.20
10.33.17obj-10.33.17.38
The two files are created by grep command in my script earlier. This behavior is also present when I used variables with the command.
Also when I try to specify a delimiter only the second file gets pasted.
Does anyone know what might be the issue? Thanks
The issue was with line endings, for some reason Windows were set up but Unix were needed.
This question already has answers here:
How can I extract a predetermined range of lines from a text file on Unix?
(28 answers)
Closed 3 years ago.
IN order to display from line 1 million, for 20 lines, I ended up with
head -1000020 myfile | tail - 20
That works and I’ve wrapped in a function that does arithmetic for it. But I was wondering if there’s an existing bash or unix command for it.
Am on macOs, but happy knowing Linux for it too.
awk to the rescue!
$ awk -v n=1000000 'NR>n; NR==n+20{exit}' file
will exit early, useful if your file is very large.
This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 5 years ago.
I have hundreds files need to loop through for an analysis using a bash script. One step I need to do is to split a long string and cat it as an output name. For example, suppose I have one string such like:
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final.bam
What I wanted is to rename it as two output file names such as:
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final_R1.fastq
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final_R2.fastq
The only changes are removing .bam from the original and cat _R1.fastq and _R2_fastq. Does somebody know how to realize it using bash commands?
somefile=blahblahblah.final.bam
foo "$somefile" "${somefile%.*}_R1.fastq" "${somefile%.*}_R2.fastq"
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I've been attempting to write a Bash script that automates everything needed to add a new piece of equipment to our MRTG graphs. Part of that requires me to edit a cfg file which I've read can be done with the sed command. The lines pasted below are where the error occurs when running the script giving me a "unexpected EOF while looking for matching `"' " error. town, tower, equipment, and direction are declared above. Any help in narrowing down what the problem might be would be a huge help!
newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s/$pattern/$newpattern/" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg
You need to use something other than slashes in the s/// command because there are slashes galore in the replacement text:
newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s%$pattern%$newpattern%" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg
I used % symbols instead; you can use any other character that appears neither in $pattern nor $newpattern. If need so be, you can use a control character such as Control-A; that works fine too.
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 8 years ago.
I've this code in my script:
no_forward="#net.ipv4.ip_forward=1"
forward="net.ipv4.ip_forward=1"
sed -i 's/$no_forward/$forward/' /etc/sysctl.conf
Based on the man page, -i suffix is not neccesary, but this this use I'll only have the modified file instead both of them (the file modified and the "backup", the previous one).
I need to use that vars, because I needing them after that command so It's useful to have them like that. I guess I should be wrong with the pattern string, but right now I can't find why. Or maybe the problem are the var's strings or their symbols?
Could You help me? I accept other solutions non-sed based if they use bash and don't need special commands, since I'll need to use the script in another computer without installing anymore.
Thanks for reading
If you want to be able to use vars in sed, use double quotes, so :
sed -i "s/$no_forward/$forward/" /etc/sysctl.conf