Unable to echo variable value - shell

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

Related

how to find linux process uptime in days

Can any one please let me know how can I find the uptime of a unix process in days?
I am able to get the date time of the process with below command
[zdkafadm#toplxkafp005 sa]$ ps -eo pid,lstart,cmd |grep server.properties | grep -v auto | awk 'NR==1;'|awk '{print $3" "$4" " $5" "$6}'
Oct 29 12:47:24 2017
Somehow I need the output something like 15 days.
Thanks,
Chandan
The easiest (sticking within shell) is using the 'dateutils' package. Otherwise perl has some fine date/time modules.
This is a fun solution:
ps -o etime= ${pid?} | rev |
sed -e 's/^/ .sdnoces /' -e 's/:/ ,setunim /' \
-e 's/:/ ,sruoh /' -e 's/-/ ,syad /' | rev

using makefile variable in sed command [duplicate]

This question already has an answer here:
Sed command in makefile
(1 answer)
Closed 6 years ago.
I have tried putting the following command in makefile.
#get Local Ip Address
LOCALIP=$(shell ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | awk '{print $1}') &
#get Web Url from User
#read -p "Enter Web Url:" weburl; \
sed -e "\|$LOCALIP $weburl|h; \${x;s|$LOCALIP $weburl||;{g;t};a\\" -e "$LOCALIP $weburl" -e "}" hosts.txt
When I try to execute the command, I expected to get the sed command like following:
sed -e "\|192.168.5.1 www.weburl.com|h; \${x;s|192.168.5.1 www.weburl.com||;{g;t};a\\" -e "192.168.5.1 www.weburl.com" -e "}" hosts.txt
But, I get the following,
sed -e "\|/s/$/OCALIP eburl|h; \" hosts.txt
In Makefiles, variables longer than a single character (i.e. all variables that you're likely to define) needs to be expanded with ${varname}, not $varname. The latter would result in the value of $v concatenated with the string arname, as you discovered.
I won't start to parse the rest of that Makefile as the piping looks a bit questionable.

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

Removing text in unix shell

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.

Resources