Linux- Removing from specific line number to specific line number [duplicate] - bash

This question already has answers here:
Delete lines from file with SED or AWK
(3 answers)
Closed 8 years ago.
I want to remove contents from line number 23 to line number 69 in file file.txt. Is there any command I can use to do that?
Thanks in advance

You can use sed with d command and inline editing option:
sed -i.bak '23,69d' file

Related

How can I delete all lines after a specific string from a number of files [duplicate]

This question already has answers here:
removing lines between two patterns (not inclusive) with sed
(6 answers)
How do you run a command eg chmod, for each line of a file?
(9 answers)
Closed 7 months ago.
I have n files, like:
file1:
Hannah
Lars
Test 123
1aaa
2eee
file2:
Mike
Charly
Stephanie
Earl
Test 123
3ccc
4ddd
5eee
I want to remove all rows after "Test 123" from all n files.
The number of rows to delete varies between files.
There's a very similar question How can I delete all lines before a specific string from a number of files in which sed -i.bak '1,/Test 123/d' file* works perfectly, but how can I do it for all lines after a specific string?
Thanks!
Despite the comments and closed question, nothing in other threads worked. Figured out a solution:
for FILENAME in * ; do sed -i.bak '/Test 123/q' $FILENAME ; done

How do I remove a line that does not contain a specific character? [duplicate]

This question already has answers here:
In-place edits with sed on OS X
(8 answers)
Closed 2 years ago.
For example, I want do remove all lines in a textile that do not contain the character '#'
I have already tried to use sed like so
sed '/#/!d' data.txt
What am I missing? Shouldn't this work?
I prefer using ed over the non-standard sed -i, especially if it needs to be portable:
printf "%s\n" "v/#/d" w | ed -s filename
This deletes every line that doesn't contain a #, and saves the changed file back to disc.
sed -n '/#/p' [file]
-n suppress default printing
/#/ match on # anywhere on the line
p print if it matches
Add -i for in-place editing of the file (if supplied).

Replace comma with new line in MacOS terminal bash shell script [duplicate]

This question already has answers here:
How to add new line using sed on MacOS?
(3 answers)
Closed 2 years ago.
I'm trying to replace each comma with a new line.
e.g. when I do the following
echo abc,wer | sed 's/\,/\n/g'
I hope to get
abc
wer
However, I got
abcnwer
What did I do wrong?
Based on #MarkSetchell answer above, the below works.
echo abc,wer | tr , '\n'

How to delete first column from the file in Unix [duplicate]

This question already has answers here:
Delete a column from a delimited file in linux
(7 answers)
Closed 8 years ago.
I have a file like the one below -
|A|B|C|D
|1|2|3|4
I want the result -
A|B|C|D
1|2|3|4
I have tried using cut but I'm not getting the desired output. Please suggest how the first column can be removed?
Using sed, delete the leading pipe symbol:
sed 's/^|//' file
There's an outside chance that on some versions of sed you'd need to escape the pipe. You might be able to use the over-write mode too (though not all versions of sed support that):
sed -i .bak 's/^\|//' file

Using sed Command [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Join lines based on pattern
I have the following file:
test
one
My
two
Hi
three
i need a way to use cat and sed to give the following output:
testone
Mytwo
Hithree
How can i achieve this in a single command?
Where the file "foo.txt" contains your text:
cat foo.txt | sed -e 'N;s/\n//'

Resources