No line breaks with "cat" [duplicate] - linux

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 3 years ago.
This code should read from wget2.html and output the links found. But it gives me output without line breaks.
How can I force cat to add line breaks?
chksitename=$(cat wget2.html | grep -e "$sitename" | sed -e "s/^.*\("$sitename".*jpg\).*$/\1/g" | sort | uniq)
echo $chksitename

The problem is not in the cat line but in the echo line. To get the line breaks, you need to use:
echo "$chksitename"
See also Capturing Multiple Line Output to a Bash Variable.

I think you can replace your cat/grep/sed with one sed:
sed -e -n "/$sitename/ s#^.*\("$sitename".*jpg\).*$#\1#pg" wget.html
And you can replace sort | uniq to sort -u.

You could try:
echo $chksitename | tr ' ' '\n'

Related

How to delete perticular lines and its nearby lines in Linux shell? [duplicate]

This question already has answers here:
How do I delete a matching line, the line above and the one below it, using sed?
(7 answers)
Closed 1 year ago.
I have a file named file.txt and it contains several lines containing string "NaN". How can I delete lines containing "Nan" and one line before and after it. I know sed -i '/pattern/d' file.txt can delete the matched line, but how can I delete neaby lines of the matched line.
Best regards
This is a most inelegant solution, but works. Perhaps, it will anger a UNIX guru to come and provide a real answer.
grep 'Nan' -n -C 1 target_file.txt | awk -F '[-:]' '{print $2}' | sed '2d' | paste -d, - - | sed 's/$/d/' > del_lines.sed && sed -f del_lines.sed target_file.txt > output_file.txt
fyi, The word "perticular" is misspelled, its "par-" like in golf!

how to print text between two specific words using awk, sed? [duplicate]

This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 4 years ago.
how to print text between two specific words using awk, sed ?
$ ofed_info | awk '/MLNX_OFED_LINUX/{print}'
MLNX_OFED_LINUX-4.1-1.0.2.0 (OFED-4.1-1.0.2):
$
Output required:-
4.1-1.0.2.0
Following awk may help you here.(considering that your input to awk will be same as shown sample only)
your_command | awk '{sub(/[^-]*/,"");sub(/ .*/,"");sub(/-/,"");print}'
Solution 2nd: With sed solution now.
your_command | sed 's/\([^-]*\)-\([^ ]*\).*/\2/'
Solution 3rd: Using awk's match utility:
your_command | awk 'match($0,/[0-9]+\.[0-9]+\-[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){print substr($0,RSTART,RLENGTH)}'
You may use this sed:
echo 'MLNX_OFED_LINUX-4.1-1.0.2.0 (OFED-4.1-1.0.2):' |
sed -E 's/^[^-]*-| .*//g'
4.1-1.0.2.0
This sed command removes text till first hyphen from start or text starting with space towards end.
Try this:
ofed_info | sed -n 's/^MLNX_OFED_LINUX-\([^ ]\+\).*/\1/p'
The sed command only selects lines starting with the keyword and prints the version attached to it.

Unable to use sed to replace text with shell variable [duplicate]

This question already has answers here:
Environment variable substitution in sed
(12 answers)
Closed 5 years ago.
For some reason, the answer in the post below doesn't work for me. Any thoughts?
how to use sed to replace a string in a file with a shell variable
I'm running CentOS 6.5
`NEW="new value"
cat myfile.txt | sed -e 's/old/${NEW}' <-- just replaces 'old' with '${NEW}'
cat myfile.txt | sed -e 's/old/$NEW' <-- just replaces 'old' with '$NEW'
cat myfile.txt | sed -e "s/old/${NEW}" <-- gives the error: unknown option to `s'
try taking the 's off the sed e.g
$ new=N
$ cat > j
one
two
three
$ sed -e "s/one/${new}/" j
N
two
three
for a more complete answer try this answer

How can I use grep to get the line number without the output? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use grep to report back only line numbers
I only want to see the line number. I don't need to see the remaining output.
Pipe your grep -n output, which normally looks something like:
11: stuff that matched
43: more stuff that matched
through sed to strip out the matching parts:
grep -n pattern file | sed -e 's/:.*//g'
11
43
grep -n or --line-number option will do this for you. You can find this information in the grep help file, which you can find by using grep --help or grep --help | less to read it more carefully. Also consider using the manual page: man grep
You could use awk too.
grep -n word file | awk -F: '{ print $1 }'
As #Barmar pointed out you could just use an awk one-liner as such:
awk '/regex/ { print NR }' file
Since you don't have awk you could also use cut:
grep -n word file | cut -d: -f1

How do I get sed to read from standard input? [duplicate]

This question already has answers here:
sed unknown option to `s' in bash script [duplicate]
(4 answers)
Closed 2 years ago.
I am trying
grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
and getting
unknown option to `s'
What am I doing wrong?
Edit:
As per the comments the code is actually correct. My full code resembled something like the following
grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
# my comment
And it appears that for some reason my comment was being fed as input into sed. Very strange.
use the --expression option
grep searchterm myfile.csv | sed --expression='s/replaceme/withthis/g'
use "-e" to specify the sed-expression
cat input.txt | sed -e 's/foo/bar/g'
To make sed catch from stdin , instead of from a file, you should use -e.
Like this:
curl -k -u admin:admin https://$HOSTNAME:9070/api/tm/3.8/status/$HOSTNAME/statistics/traffic_ips/trafc_ip/ | sed -e 's/["{}]//g' |sed -e 's/[]]//g' |sed -e 's/[\[]//g' |awk 'BEGIN{FS=":"} {print $4}'
If you are trying to do an in-place update of text within a file, this is much easier to reason about in my mind.
grep -Rl text_to_find directory_to_search 2>/dev/null | while read line; do sed -i 's/text_to_find/replacement_text/g' $line; done
Open the file using vi myfile.csv
Press Escape
Type :%s/replaceme/withthis/
Type :wq and press Enter
Now you will have the new pattern in your file.

Resources