Adding a separator between found results by sed - shell

I have a text file like this for example:
test.txt:
Hello my name is test
Well my name will be test
Hello Hello test
Hello my name already is test
Now I want to get everything between every 'Hello' and 'test'. This works for me:
cat test.txt | sed --quiet '/Hello/,/test/p'
It gives the following output:
Hello my name is test
Hello Hello test
Hello my name already is test
Would it be possible to separate my findings like this:
Hello my name is test;
Hello Hello test;
Hello my name already is test;
The delimiter does not have to be ';' any other character will work just fine.

Your question is unclear, but, assuming you want lines that start with Hello and to add a semi colon at the end, you can try;
$ sed -n '/^Hello/{s/$/;/p}' input_file
Hello my name is test;
Hello Hello test;
Hello my name already is test;

Related

How to Search for a word only in first line of file in unix

i want to search a word in only first line of file in unix and if word is not present then insert a line before and in the last of the file
for eg
demo.txt file contains
I am looking for something
ABC
DEF
this is demo file
suppose i want to check in first line i am looking for word 'request'
here in this file request is not in first line so a new line before current line should be added
demo.txt should contain now
added request
I am looking for something
ABC
DEF
this is demo file
added request last
Above we can see first and last line are added
any suggestions, i had used sed
code:
[ "$(sed -n '1p' demo.txt)"=="request" ]
but above code does not work
When wanting to automate editing a file in a script, ed is often a better choice than sed - especially if you want to write multiple things to multiple different locations, as it's easy to move the current-line cursor around to arbitrary places. Example:
$ cat demo.txt
I am looking for something
ABC DEF this is demo file
$ ed -s demo.txt <<'EOF'
1v/request/i\
added request\
.\
$a\
added request last\
.\
w
EOF
$ cat demo.txt
added request
I am looking for something
ABC DEF this is demo file
added request last
Translation:
If the first line does not contain the string request (1v/request/), insert text before the first line, and append text after the last line, and finally write the changed file back to disk. The backslashes at the end of every line but the last are needed to treat the entire thing as a single list of commands passed to v.
Is it what you want?
To edit file, just add -i option.
GNU sed only.
$ cat demo.txt
I am looking for something
ABC DEF this is demo file
$ cat demo2.txt
Blah Blah request Blah
ABC DEF this is demo file
$ sed -n '1{/request/!{h;s/.*/added request/;p;x;}};p;${g;/added request/{s/.*/& last/p}}' demo.txt
added request
I am looking for something
ABC DEF this is demo file
added request last
$ sed -n '1{/request/!{h;s/.*/added request/;p;x;}};p;${g;/added request/{s/.*/& last/p}}' demo2.txt
Blah Blah request Blah
ABC DEF this is demo file

Shell Script for merging multiple text files into One file fails

Need help in merging multiple text files in to one file,
When am doing this through shell script it is changing the alignment of the file.
Eg : File 1 has data as below :
Hello world
Hello World1
Hello World2
File 2 has data as below :
Hello New World
Hello New World 2
Resultant file created through shell script post merging :
Hello world Hello Wor
ld Hello world2
the lines of the files are clubbed together.
This shell script is executed on the AS400 system
Code used :
cat *.${3} >> ${2}
Try this..
cat file1
`Hello world
Hello World1
Hello World2`
cat file1 >> file3
cat file3
Hello world
Hello World1
Hello World2
cat file2
Hello New World
Hello New World 2
cat file2 >> file3
cat file3
Hello world
Hello World1
Hello World2
Hello New World
Hello New World 2
Another way out is :$ sed -n wfile.merge file1.txt file2.txt
I will assume that :
1 - ${3} is the extension of the files you want to concatenate, and therefore *.${3} is the names of all the source files.
2 - The result you posted is not actually an exact output. (there is no "world2" word in the source files)
What comes to mind is that the line terminations of the source files might be causing the error. Perhaps some '\r' are present there ? You should check that the line endings are unix-style (lines end with '\n')
Otherwise, try running cat File1 and cat File2 and check that the output is as expected. This should give you a hint as to why they are catted wrong

Bash grep - find exact wording in the line

I have a question using grep / egrep in bash
Imagine I have the following lines in the file:
Good day
Hello World 23 a
Hi Hello World
Hello World
sdHello World
Hello Worldsss
The problem is to find the line with exact match of string: "Hello World"
I tried to use
grep -n "^Hello World$" file.txt
but it doesn't work giving an empty result
If you want to look for exact matches, rather than matching a regular expression, use -F to specify that you're searching for a fixed string.
And to make it match the whole line instead of a substring, use -x.
grep -n -F -x "Hello World" file.txt
But your ^Hello World$ regexp should have worked. If it's not matching,then the file may not contain what you think it does. One possibility is that it contains Windows CRLF newlines instead of Unix LF newlines. Use dos2unix file.txt to fix it.

How to add string to empty lines of file in scripting?

Example : file1 has data like
abc
cab
def
xxy
zay
sri
ram
In this file 3rd,7th,9th lines are empty, how to fill this empty lines with Specific string?.
For example if i want to fill these lines with Hello
Output File should be like:
abc
cab
Hello
def
xxy
zay
Hello
sri
Hello
ram
sed 's/^$/Hello/' file1
will output what you want.
You can redirect that to the output file as below.
sed 's/^$/Hello/' file1 > file2
If you want to change the original file itself, you can use the -i option.
sed -i 's/^$/Hello/' file1

Bash: Changing a specific line in a file, to something which includes the line

I'm trying to change a file like this:
Hello
My name is [Bob]
My age is 34
Should turn into:
Hello
My name is [Bob]SomeText[Bob]SomeText
My age is 34
However, to make it easier, I know that the 'string' i'm looking for will ALWAYS start with
[Bo
Any help will be appreciated, if you need more info, just say :)
If you don't really need to use bash only, you can use sed:
sed 's/\[Bo.*/&SomeText&SomeText/g' filename
output:
Hello
My name is [Bob]SomeText[Bob]SomeText
My age is 34
Use sed. Match everything form [Bo up to the first ] and replace it:
sed 's/\(\[Bo[^\]]*\]\)/\1SomeText\1/'
perl -pe 'm/(\[Bo.*?\])/g;$a=$1;s/(.*\[Bo.*?\])/$1sometext${a}sometext/g;' your_file
Above solution works provided your [Bo.*] will always be present at the end of the line.
tested:
> cat temp
Hello
My name is [Bob]
My age is 34
> perl -pe 'm/(\[Bo.*?\])/g;$a=$1;s/(.*\[Bo.*?\])/$1sometext${a}sometext/g;' temp
Hello
My name is [Bob]sometext[Bob]sometext
My age is 34
>

Resources