Removing text in unix shell - macos

Sorry, I'm pretty new to coding. I'm just trying to remove the CST that follows the end of the string. The final output that I'm trying to get says "Sunset: 4:38 PM CST". Exclude the quotation marks.
Here is the code that I'm using within the shell.
curl http://m.wund.com/US/MN/Winona.html | grep 'Sunset' | sed -e :a -e 's/<[^>]*>//g;/</N;//ba' | sed -e 's/Sunset/Sunset: /g' | sed -e 's/PST//g'

Just change:
... | sed -e 's/PST//g'
to
... | sed -e 's/CST//g'
You might also want to invoke curl -s instead of just curl to omit all the downloading stuff.

Related

Using sed to replace tabs if input is not guaranteed to contain tabs?

I'm trying to extract a list of names from a website using sed, but I'm not sure how to go about replacing the tab characters separating them.
This code:
curl -s "https://namnidag.se/?year=2022&month=9&day=12" | sed -nE -e "s#<div class='names'>([^<]*)</div>#\1#p" | html2text
gives me the names for September 12th, but they are separated by a tab character:
Åsa Åslög
If I change the sed script to replace tabs with comma and space, like this:
curl -s "https://namnidag.se/?year=2022&month=9&day=12" | sed -nE -e "s#<div class='names'>([^<]*)</div>#\1#" -e 's/\t/, /p' | html2text
it works as expected:
Åsa, Åslög
However, if I try on a day that only has one name, such as September 13th:
curl -s "https://namnidag.se/?year=2022&month=9&day=13" | sed -nE -e "s#<div class='names'>([^<]*)</div>#\1#" -e 's/\t/, /p' | html2text
I get no output; the first sed script without the tab replacement works fine in this case though. What am I doing wrong here?
I'm using GNU sed 4.8, if that helps.
Thanks!
You need to remove the p
curl -s "https://namnidag.se/?year=2022&month=9&day=12" | sed -nE -e "s#<div class='names'>([^<]*)</div>#\1#p" | sed -e 's/\t/, /'
curl -s "https://namnidag.se/?year=2022&month=9&day=12" > f1
cat > ed1 <<EOF
71W f2
q
EOF
ed -s f1 < ed1
cat f2 | tail -c +20 | head -c -6 > file
rm -v ./ed1
rm -v ./f2
This will give you the names, whether there are two of them or not; and if there are, you can just seperate them with cut.

Replace file line with multi-line, special char string

I'm trying to automate generating a README.md.
The idea is:
Generate markdown table string like...
table="| Image | Name | Description | Notes |\n"
table+="| --- | --- | --- | --- |\n"
table+="| $img1 | $name1 | $desc1 | $notes1 |\n"
table+="| $img2 | $name2 | $desc2 | $notes2 |\n"
...
*simplified
*contains special characters like e.g. |-()[]/<>
Replace <!-- insert-table-here --> in a readme_template.md file with the full table
## Header
<!-- insert-table-here -->
<sub>More info...</sub>
Save new file as README.md
I can't get step 2 working.
How do you replace a line in a file with a multi-line, special char ridden string?
Every sed, awk, perl, or even head/tail command I try seems to not work. Are heredocs the better approach?
I have found some hack solutions for specific cases with specific chars but I want to identify a more robust method.
EDIT: Thanks to #potong, this is what ended up working for me.
echo -e ${table} | sed -e '/<!-- insert-table-here -->/{r /dev/stdin' -e 'd}' readme_template.md > README.md
EDIT 2: After spending some more time on this, I found a nice multi-match option through awk
awk \
-v t1="$(generate_table1)" \
-v t2="$(generate_table2)" \
'{
gsub(/<!-- insert-table-1 -->/,t1)
gsub(/<!-- insert-table-2 -->/,t2)
}1' \
readme_template.md > README.md
This might work for you (GNU sed and bash):
cat <<\! | sed -e '/<!-- insert-table-here -->/{r /dev/stdin' -e 'd}' file
Here is a heredoc
with special symbols
{}-()[]/<>
!
The heredoc is piped through to the sed command using /dev/stdin as a file for the r command, then the original line is deleted using the d command.
N.B. The use of the -e command line option to split the two parts of the sed script (oneliner). This is necessary because the r command needs to be terminated by a newline and the -e option provides this functionality.

Unable to echo variable value

Assigning the output of sed command in a variable but unable to print its value, the command works fine:-
uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'
but I assigned a variable for its out like below:-
abc=uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'
and calling variable is not pulling the value.
Tried like below:-
echo {"$abc"}
printf "$abc"
echo "${abc}"
Kindly suggest the syntax for output.
abc=uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'
Actually we need to pull the uptime value for only number of days on AIX server and call that value to form a report of servers which will show number of days server uptime for AIX servers. Need to know how to call the variable value and embedd it in a shell script.
Depends on your shell, but for most sh-ish variants:
abc=$(uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g')
Because you are not storing the final evaluation in abc.
Try
abc=$(uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g')

Shorten sed sustitution or possible alternative

I've some data being fed in few files. The requirement is to format the textual contents in these files and add newlines post formatting.
Requirement of substitution:
Text | Substituted
-----------------------
#Network | #Network
# Network | #Network
#Daemon | #Daemon
# Daemon | #Daemon
#Service | #Service
# Service | #Service
----------------------
I've tried using sed to do this, but the command gets huge and cluttered, as the substitution is not limited to only letters N,D & S and more and more Capital Alphabets gets added day by day in the requirement.
cat results_090316.out | sed -e 's/ //g' -e 's/#N/#N/g' -e 's/#S/#S/g' -e 's/#D/#D/g' -e 's/# N/#N/g' -e 's/# S/#S/g' -e 's/# D/#D/g' | tr '#' '\n'
If sed is not the proper tool to perform such substitutions, could you suggest an alternative?
The code is written in bash on RHEL 6 / Solaris 10 OS.
You can shorten it using a character class and optional space matching:
sed 's/ //g; s/# *\([NDS]\)/#\1/g' results_090316.out
Your choice of tool is alright, but you're not using the full power of regular expressions. For example, below I use a "character class" to create a custom group of characters to match, e.g. [NSD], and then use a "backreference" (\1) by first "capturing" a piece of the search (with \( and \)):
cat results_090316.out | sed -e 's/ //g' -e 's/#\([NSD]\)/#\1/g' -e 's/# \([NSD]\)/#\1/g' | tr '#' '\n'
But we can do better and use the ? "quantifier" (zero or one of the predecessing atom) to combine even the no-space and space cases:
cat results_090316.out | sed -e 's/ //g' -e 's/# \?\([NSD]\)/#\1/g' | tr '#' '\n'

sed: file skripta.txt line 1: unknown option to `s'

I try to use:
sed -e 's/miza/stol/g' datoteka1.txt | sed -e '/klop/d' | sed -e '/^$/d' | sed -e 's/janez/Janez/g'
in a file named skripta.txt with "sed -f skripta.txt > datoteka2.txt" to save it in another file and I get this error mentioned in title.
If I run this code seperately it works just fine.
What is wrong here???
This is a shell script that uses sed, not a sed script.
Run it with bash skripta.txt > datoteka2.txt
So let me get this straight:
You have a file called skripta.txt that contains only this line:
sed -e 's/miza/stol/g' datoteka1.txt | sed -e '/klop/d' | sed -e '/^$/d' | sed -e 's/janez/Janez/g'
And you try to run it with
$ sed -f skripta.txt
Is that correct?
The error is not surprising then, because it expects just the sed commands, not a call to sed itself, inside the script file.
It interprets the initial s of the first sed as 'search and replace', but the following syntax ed doesn't match.
You can either change the skripta.txt into a shell script: (You could also change it's name into skripta.sh):
#!/usr/bin/env bash
sed -e 's/miza/stol/g' datoteka1.txt | sed -e '/klop/d' | sed -e '/^$/d' | sed -e 's/janez/Janez/g'
change it's mode to executable:
$ chmod u+x skripta.sh
Then you can just call it:
$ ./skripta.sh
Or you can turn it into a sed script by removing all the seds:
s/miza/stol/g
/klop/d
/^$/d
s/janez/Janez/g
Then you can run it with
$ sed -f skripta.txt < datoteka1.txt > datoteka2.txt

Resources