How can I print lines using sed Command? - sed

Can someone tell me a 'one line' sed command that prints out the lines 100-104 and 205-210 of of any file that has 500 lines in total ?
I tried this one :
sed -n '100,104'p unique_undetermined

#Sundeep is suggesting the following:
sed -n '100,104p;205,210p' unique_undetermined

Related

SED command to the following

Using SED I would like to transform several hundred lines in a text file from:
Input example:
https://mysite.demo.com/topics/en-gb/3
https://mysite.demo.com/topics/en-gb/436
https://mysite.demo.com/topics/en-gb/9167
into
Output:
https://mysite.demo.com/topics/en-gb/3/pdf/3.pdf
https://mysite.demo.com/topics/en-gb/436/pdf/436.pdf
https://mysite.demo.com/topics/en-gb/9167/pdf/9167.pdf
I was wondering what SED command I would use to do this?
Many thanks
Run: echo "https://mysite.demo.com/topics/en-gb/3" |\
sed "s|\(https:\/\/mysite.demo.com\/topics\/en-gb\)\/\([0-9]\+\)|\1/\2/pdf/\2.pdf|g"
Output:
https://mysite.demo.com/topics/en-gb/3/pdf/3.pdf
Here I use sed "s|||" instead of sed "s///".
As per your sample input and expected output, this sed command would work:
sed -E 's,(.*\/)([0-9]+$),\1\2\/pdf\/\2\.pdf,g' text_file
Output:
https://mysite.demo.com/topics/en-gb/3/pdf/3.pdf
https://mysite.demo.com/topics/en-gb/436/pdf/436.pdf
https://mysite.demo.com/topics/en-gb/9167/pdf/9167.pdf

manipulation of text by sed command

I a file containing the genome ids following NZ_FLAT01000030.1_173 I need to manipulate those ids like this one: NZ_FLAT01000030.1
I tried some but didn't give me the exact thing.
sed 's/_/\t/' output : NZ FLAT01000030.1_173
sed -r 's/_//' output: NZFLAT01000030.1_173
sed -r 's/_//g' output: NZFLAT01000030.1173
How can I do that by using sed command?
Are you trying to remove the undesrscore and the digits following it?
echo 'NZ_FLAT01000030.1_173' | sed -E 's/_[0-9]+//g'
NZ_FLAT01000030.1
$ echo 'NZ_FLAT01000030.1_173' | sed 's/_[^_]*$//'
NZ_FLAT01000030.1

Better way to fix mocha lcov output using sed

Due to the know prob of mocha-lcov-mocha breaking file paths, I need to fix the current output paths that looks like this:
SF:Vis/test-Guid.coffee
SF:Vis/Guid.coffee
SF:Vis/test-Vis-Edge.coffee
SF:Vis/Vis-Edge.coffee
into
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
I'm not very good with sed, but I got it to work using:
mocha -R mocha-lcov-reporter _coverage/test --recursive | sed 's,SF:,SF:src/,' | sed s',SF.*test.*,SF:test//&,' | sed s',/SF:,,' | sed s',test/src,test,' | ./node_modules/coveralls/bin/coveralls.js
which is basically doing 4 sed commands in sequence
sed 's,SF:,SF:src/,'
sed s',SF.*test.*,SF:test//&,'
sed s',/SF:,,'
sed s',test/src,test,'
my question is if there is a way to do with this one sed command, or use another osx/linux command line tool
Initially put "src/" after every ":" and then if "test" is found on the line replace "src" with "test":
$ sed 's,:,:src/,;/test/s,src,test,' file
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
You could put all the sed commands in a file, one line per command, and just use "sed -e script". But if you just want it on a single command-line, separate with semicolons. This works for me:
sed 's,SF:,SF:src/,;s,SF.*test.*,SF:test//&,;s,SF:,,;s,test/src/,test,'
sed command
sed '\#test#!{s#SF:Vis/#SF:src/Vis/#g};\#SF:Vis/test#{s#SF:Vis/test#SF:test/Vis/test#g};' my_file
Here is an awk version:
awk -F: '/SF/ {$0=$1FS (/test/?"test/":"src/")$2}1' file
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
How it works:
awk -F: ' # Set field separator to ":"
/SF/{ # Does line start with "SF"?
$0=$1FS (/test/?"test/":"src/")$2 # Recreat String by adding "test" if line contains "test", else "src"
}
1 # Print all lines
' file # read the file

Delete first and last line or record from file using sed

I want to delete first and last line from the file
file1 code :
H|ACCT|XEC|1|TEMP|20130215035845|
849002|48|1208004|1
849007|28|1208004|1
T|2
After delete the output should be
849002|48|1208004|1
849007|28|1208004|1
I have tried below method but has to run it 2 times, I want one liner solution to remove both in one go!
sed '1,1d' file1.txt >> file1.out
sed '$d' file1.out >> file2
Please suggest one liner code....
You could use ;
sed '1d; $d' file
Use Command Separator
In sed, you can separate commands using a semicolon. For example:
sed '1d; $d' /path/to/file
How about:
sed '$d' < file1.txt | sed "1d"
Try sed -i '1d;$d' /path/to/file
awk 'NR>2{print v}{v=$0}'
Starting with line 3, print the previous line each time. This means the first and last lines will not be printed.

using sed to get the xth, yth, and zth line of a text file

I know you can use sed to get the nth line of a text file as follows:
sed -n '30p' foo.txt
will output the 30th line of foo.txt
However, suppose I'm interested in the 30th, 39th, 43rd lines of foo.txt? Is there a way to string this together in sed?
Thanks.
Sure is...
sed -n '30p;39p;43p' foo.txt
If they are in a contiguous range, say 39-42 you can do something like this:
sed -n '39,42p' foo.txt