This question already has answers here:
how to remove the first two columns in a file using shell (awk, sed, whatever)
(9 answers)
Closed 4 years ago.
From a text file, I want to remove the first 6 columns. I tried sed as follows, but I have to do it six times (one for each column). Is there any efficient way to do it (or pass the 6 columns at once for sed)?
sed -i -r 's/(\s+)?\S+//1' file
Thanks!
You could do this within regex and quantifying braces if a column consists of non-whitespace characters with optional leading spaces:
sed -i -r 's/^((\s+)?\S+){6} *//' file
Related
This question already has answers here:
Delete first and last line or record from file using sed
(5 answers)
Closed 4 years ago.
I'm using sed command to edit some text file.
How can i exclude the first and last line in each text file for being edited.
I went through sed gnu manual but i only found commands to match line ranges i.e. 1,$ or to exclude ranges i.e. 1,$!. i just need to exclude line # 1 and last line $. i'm not sure if its possible to select a range i.e 2, $-1?
Here's my code.
sed -e '1,$ s/.*/<p>&<\/p>/'' file.txt
You can't use maths in sed addresses. But you can tell sed to do nothing on the first and last line:
sed -e '1n; $n; s/.*/<p>&<\/p>/'
where n means "read the next line of input into the pattern space" (in case of the last line, it won't read anything).
(The two single quotes at the end of the expression are probably a typo, right?)
This question already has answers here:
exchange two words using sed
(5 answers)
Closed 5 years ago.
I'm trying to write a shell script that switches the first and third words in a line. In this case only strings that contain letters (both upper- and lowercase) count as words, everything else (numbers, punctuation, whitespace) is considered whitespace.
For example:
abc123def. ghi...jkl
would turn into:
ghi123def. abc...jkl
I tried the following, but it doesn't work:
sed 's/\([a-zA-Z][a-zA-Z]*\)[^A-Z^a-z]\([a-zA-Z][a-zA-Z]*\)[^A-Z^a-z]\([a-zA-Z][a-zA-Z]*\)/\3 \2 \1/' input.txt
With sed:
$ echo "abc123def. ghi...jkl" | sed -r 's/([A-Za-z]*)([^A-Za-z]*[A-Za-z]*[^A-Za-z]*)([A-Za-z]*)(.*)/\3\2\1\4/g'
$ ghi123def. abc...jkl
This question already has answers here:
How to delete from a text file, all lines that contain a specific string?
(21 answers)
Closed 6 years ago.
For example I have a caf.txt file and I want to delete a "donut" word in the document without entering the document on linux .How can I do it?
To delete just the word "donut"
sed -i 's/donut//g' caf.txt
To delete lines that contain the word "donut"
sed -i '/donut/d' caf.txt
What I do is:
sed '/text_to_delete/d' filename | sponge filename
This will make the change to the source file.
This question already has answers here:
Replace whitespace with a comma in a text file in Linux
(9 answers)
Closed 6 years ago.
I have a tab separated text file f.txt like :
APPLE 10 5
BALL 20 6
CAT 30 7
I want the output to be
APPLE_10_5
BALL_20_6
CAT_30_7
I wrote the following to partially accomplish this, but I am stuck at the "paste" step. Can you help?
cat f.txt | cut -f 1,2,3 | paste ???
When the are sperated by one space using sed is a one liner.
sed -i "s/ /_/" input.txt
This question already has answers here:
How do I use a new-line replacement in a BSD sed?
(4 answers)
Closed 7 years ago.
sed '/^;date.timezone =/!b;:a;n;//ba;i\date.timezone = Europe/London' /etc/php.ini
You can probably guess Im creating a script for setting up LAMP servers.
In the above example the text is not replaced but instead the changes are displayed on the console.
I my goal was to insert date.timezone = Europe/London the last occurence of ;date.timezone =
Etan Reisner:
You aren't using the -i flag to tell sed to modify in place.