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

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

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

Sed and delete lines matching "String1 AND string 2"

I can't find how to make that, I want to remove every line containing ACE and REE. only if both words are present.
sed -i '/ACE/d' $1
I would suggest using awk, which would allow you to specify both patterns separately:
awk '!(/ACE/ && /REE/)' file
All lines are printed, except for those where both patterns match.
The advantage of this approach is that it would work regardless of the order in which the two strings appear.
To achieve an "in-place" edit, you can go for the standard approach:
awk '!(/ACE/ && /REE/)' file > tmp && mv tmp file
i.e. output to a temporary file and then overwrite the source.
Try this method
sed -i '/ACE.*RRE/d' FileName
Or
sed -i '/\(ACE\|CH3\).*\(CH3\|ACE\)/d' FileName
Example:
cat sample
Output:
336 ACE CH3 1.00
123 ACE 321 test
This ACE for testing CH3
Command:
sed '/ACE.*CH3/d' sample
Output:
123 ACE 321 test
If you need to match both words but you don't know the order you need to try both cases like so:
sed -i '/\(ABC.*DEF\)\|\(DEF.*ABC\)/d' FileName
It will match any row with either ABC.*DEF or DEF.*ABC and then remove it.
NOTE: With the pattern similar to the following you will also match the case where ABC occurs 2 times and DEF 0 times, and that is not what you want.
sed -i '/\(ABC\|DEF\).*\(ABC\|DEF\)/d' FileName

How to add text before existing text

Hello I would like to add the word: tarvell: before each word on every line.
The file is 1Gb so I would like to be able to do it through my VPS if possible.
Im guessing some sort of grep/awk program could do it. Thanks.
Current:
Line1
Line2
Line3
Want:
tarvellLine1
tarvellLine2
tarvellLine3
Q: Im guessing some sort of grep/awk program could do it
A: Yes, indeedy.
I'm guessing your VPS is probably running Linux (or a close *nix variant), and that you have a command prompt (e.g. a putty terminal).
Try "sed".
EXAMPLE:
# Create input file
vi tmp.txt
abc
def
geh
# Add "tarvell" to the beginning of each line; write to a second file
sed 's/^/tarvell/' tmp.txt > tmp2.txt
# Print the results
cat tmp2.txt
tarvellabc
tarvelldef
tarvellgeh
sed 's/\(^\|\W\)\(\w\)/\1tarvell\2/g' -i filename
original
one two
three (four), five
updated
tarvellone tarvelltwo
tarvellthree (tarvellfour), tarvellfive

Use sed to extract ascii hex string from a file

I have a file that looks like this:
$ some random
$ text
00ab2c3f03$ and more
random text
1a2bf04$ more text
blah blah
and the code that looks like this:
sed -ne 's/\(.*\)$ and.*/\1/p' "file.txt" > "output1.txt"
sed -ne 's/\(.*\)$ more.*/\1/p' "file.txt" > "output2.txt"
That gives me this 00ab2c3f03 and this 1a2bf04
So it extracts anything from the beginning of the line to the shell prompt and stores it in the file, twice for two different instances.
The problem is that the file sometimes looks like this:
/dir # some random
/dir # text
00ab2c3f03/dir # and more
random text
345fabd0067234234/dir # more text
blah blah
And I want to make an universal extractor that either:
extracts data from the beginning of the line to the '$' OR '/' characters
intelligently extracts random amount of random hex data from the beginning of the line up to the first non-hex digit
But I'm not so good with sed to actually think of an easy solution by myself...
I think you want the output like this,
$ cat file
$ some random
$ text
00ab2c3f03$ and more
random text
1a2bf04$ more text
blah blah
/dir # some random
/dir # text
00ab2c3f03/dir # and more
random text
345fabd0067234234/dir # more text
blah blah
$ sed -ne 's/\([a-f0-9]*\).* and more.*/\1/p' file
00ab2c3f03
00ab2c3f03
$ sed -ne 's/\([a-f0-9]*\).* more text.*/\1/p' file
1a2bf04
345fabd0067234234
You could try the below GNU sed command also. Because / present in your input, i changed the sed delimiter to ~,
$ sed -nr 's~([a-f0-9]*)\/*\$*.* and more.*~\1~p' file
00ab2c3f03
00ab2c3f03
$ sed -nr 's~([a-f0-9]*)\/*\$*.* more text.*~\1~p' file
1a2bf04
345fabd0067234234
Explanation:
([a-f0-9]*) - Captures all the hexdigits and stored it into a group.
OP said there may be chance of / or $ symbol present just after the hex digits so the regex should be \/*\$*(/ zero or more times, $ zero or more times) after capturing group.
First command only works on the lines which contains the strings and more.
And the second one only works on the lines which contain more text because op want the two outputs in two different files.
This seems better to me:
sed -nr 's#([[:xdigit:]]+)[$/].*#\1#p' file

Resources