Using sed Command [duplicate] - bash

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//'

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

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'

Bash. How to assign random line from text file to a variable? [duplicate]

This question already has answers here:
What's an easy way to read random line from a file?
(13 answers)
Closed 3 years ago.
I have text file which contains numerous lines. I need to do:
shuf txt.txt
From shuf output read first line to a variable $line
How to represent it in one line for bash script?
Now it is like this:
shuf txt.txt -o aaa.txt
n=$(head -n 1 aaa.txt)
rm -rf aaa.txt
As you may notice, it is not very nice
You can easily do this with shuf:
n=$(shuf -n1 file)
↳ https://www.gnu.org/software/coreutils/manual/html_node/shuf-invocation.html
Related question on stack overflow.com: What's an easy way to read random line from a file in Unix command line?
Sounds like homework. Here's some hints.
Assigning a variable to the result of a command:
x=`command`
Assignment with pipe:
x=`command1 | command2`
Assigning the first line of command1's output to a variable:
x=`command1 | head -n1`

How to "grep" lines of a file by range of value [duplicate]

This question already has answers here:
Grep inside all files created within date range
(3 answers)
Closed 5 years ago.
I have a file with timestamps and I want to grep it so that I can see the lines matching 12:30:00 - 12:32:00 . I know that in bash there is the {0..2} for doing something similar but I dont know in grep how to do it?
At the moment I am doing grep 2015-01-12 12:3{0..2} but it doesnt work?
you can use the following for this.
grep '2015-01-12 12:3[0-2]'
or
grep '2015-01-12 12:3[0,1,2]'
I hope it helps.

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

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

Resources