Using the head Command The head command is used to display the first lines of a file. By default, the head command will print only the first 10 lines.
Just add the line number before: sed '<line number>s/<search pattern>/<replacement string>/ . Note I use . bak after the -i flag. This will perform the change in file itself but also will create a file.
Beginning of line ( ^ ) In grep command, caret Symbol ^ matches the expression at the start of a line.
Don't use sed
, use cut
:
grep .... | cut -c 1-N
If you MUST use sed
:
grep ... | sed -e 's/^\(.\{12\}\).*/\1/'
colrm x
For example, if you need the first 100 characters:
cat file |colrm 101
It's been around for years and is in most linux's and bsd's (freebsd for sure), usually by default. I can't remember ever having to type apt-get install colrm
.
don't have to use grep either
an example:
sed -n '/searchwords/{s/^\(.\{12\}\).*/\1/g;p}' file
Strictly with sed:
grep ... | sed -e 's/^\(.\{N\}\).*$/\1/'
To print the N first characters you can remove the N+1 characters up to the end of line:
$ sed 's/.//5g' <<< "defn-test"
defn
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With