Passing a date -d to sed command - date

I got a script looking like that :
function tvpsport(){
wget -qO- 'https://sport.tvp.pl/'|
grep -i -B 9 'video_id'|
grep 'title\|broadcast_start\|video'|
sed -e 's/"//g' \
-e 's/,$//g' \
-e 's/\\u2013/-/g' \
-e 's/\\u0105/ą/g' \
-e 's/\\u0119/ę/g' \
-e 's/\\u0142/ł/g' \
-e 's/\\u00f3/ó/g' \
-e 's/\\u015a/Ś/g' \
-e 's/\\u017a/ź/g' \
-e 's/\\u017c/ż/g' \
-e 's/\\u0144/ń/g' \
-e 's/title : //g' \
-e "s/broadcast_start : /$(date -d) /g" \
-e 's/video_id : /https:\/\/tvp.pl\/sess\/TVPlayer2\/embed.php?ID=/g'
}
which gives output :
Title of transmission
date -d {time format}
a link to transmission
I want to express that format date to human readable (from eg. 161145246842 to dd/mm/YYYY)
and output like :
date (dd/mm/YYYY) title of transmission
a link to a transmission
I tried to covrt date on this line :
-e 's/broadcast_start : /date -d /g' \
but with no luck

Related

problems while reading log file with tail -n0 -F

i am monitoring the asterisk log file for peers that get offline.
the if part is working correct, but the sed command is not executed in the else part, although the echo command works. What do i need to change
tail -n0 -F /var/log/asterisk/messages | \
while read LINE
do
if echo "$LINE" | /bin/grep -q "is now UNREACHABLE!"
then
EXTEN=$(echo $LINE | /bin/grep -o -P "(?<=\').*(?=\')")
echo "$EXTEN is now UNREACHABLE!"
CALLERID=$(/bin/sed -n '/^\['"$EXTEN"'\]/,/^\[.*\]/{/^callerid*/p}' "$SIP" | /usr/bin/awk -F'=' '{ print $2 }')
if .......
then
.......
fi
elif echo "$LINE" | /bin/grep -q "is now REACHABLE!"
then
EXTEN=$(echo $LINE | /bin/grep -o -P "(?<=\').*(?=\')")
echo "$EXTEN is now REACHABLE!"
if /bin/grep -qi "^$EXTEN;" $OFFLINE; then
/bin/sed -i '/^$EXTEN;/d' $OFFLINE
fi
fi
done
You have a quoting problem - you've used single quotes when the string includes a shell variable:
if /bin/grep -qi "^$EXTEN;" $OFFLINE; then
/bin/sed -i '/^$EXTEN;/d' $OFFLINE
fi
Try using double quotes instead:
if /bin/grep -qi "^$EXTEN;" $OFFLINE; then
/bin/sed -i "/^$EXTEN;/d" $OFFLINE
fi

How to insert multiple complex lines containing spaces, pipes, grep and sed commands before pattern

The goal is to insert the following complex lines before a specific pattern in a file:
NDPI_VERSION_SHORT=$(cat Makefile | grep -P "^NDPI_VERSION_SHORT = " | sed -E 's|^NDPI_VERSION_SHORT = (.*)$|\1|g') \
NDPI_VERSION_SHORT=${NDPI_VERSION_SHORT//[[:space:]]/} \
NDPI_MAJOR=$(cat Makefile | grep -P "^NDPI_MAJOR = " | sed -E 's|^NDPI_MAJOR = (.*)$|\1|g') \
NDPI_MAJOR=${NDPI_MAJOR//[[:space:]]/}
I unsuccessfully tried the following:
sed -i '/pattern/i \
NDPI_VERSION_SHORT=$(cat Makefile | grep -P "^NDPI_VERSION_SHORT = " | sed -E \'s|^NDPI_VERSION_SHORT = (.*)$|\1|g\') \
NDPI_VERSION_SHORT=${NDPI_VERSION_SHORT\/\/[[:space:]]\/} \
NDPI_MAJOR=$(cat Makefile | grep -P "^NDPI_MAJOR = " | sed -E \'s|^NDPI_MAJOR = (.*)$|\1|g\') \
NDPI_MAJOR=${NDPI_MAJOR\/\/[[:space:]]\/}' file
bash: syntax error near unexpected token `('
I also tried to quote all inserted lines leading to the same result.
What am I doing wrong?
This should work:
sed "/pattern/i \
NDPI_VERSION_SHORT=\$\(cat Makefile | grep -P \"^NDPI_VERSION_SHORT = \" | sed -E 's|^NDPI_VERSION_SHORT = \(.*\)\$|\\\1|g'\) \\\ \n\
NDPI_VERSION_SHORT=\${NDPI_VERSION_SHORT//[[:space:]]/} \\\ \n\
NDPI_MAJOR=\$\(cat Makefile | grep -P \"^NDPI_MAJOR = \" | sed -E 's|^NDPI_MAJOR = \(.*\)\$|\\\1|g'\) \\\ \n\
NDPI_MAJOR=\${NDPI_MAJOR//[[:space:]]/}" file
The problem is the single quote within the inserted text, which will end the sed script and which cannot be escaped. You can use single quotes, though, if you use double quotes to enclose the script. This, however, means you'll need to escape quite a lot of things in your text: The $, ", (, ). Since the shell itself uses up a backslash for escaping, you need to write \\\ where you have a \. And the line break is achieved via a \n. Note that the / does not need to be escaped since sed does not use it as delimiter here.

Logs extracting, limiting cpulimit

I have the following script for extraction of logs.
Need to make work it with cpulimit, to not overload the server. Can anyone help?
nice -20 zcat /var/detail-20150418.gz | sed '/./{H;$!d;};x;/46.241.178.96/!d' > /tmp/new.txt
nice -20 sed -n '/Acct-Status-Type/,/Called-Station-Id/p' /tmp/new.txt > /tmp/new_2.txt
rm /tmp/new.txt
nice -20 sed '/Acct-Status-Type/{x;p;x;}' /tmp/new_2.txt > /tmp/new_3.txt
rm /tmp/new_2.txt
grep -v '\(Acct-Authentic\|Acct-Input-Octets\|Acct-Input-Gigawords\|Acct-Output-Octets\|Acct-Output-Gigawords\|Acct-Input-Packets\|Acct-Output-Packets\|Acct-Session-Time\)' /tmp/new_3.txt > /tmp/new_4.txt
rm /tmp/new_3.txt
sed 's/^[ \t]*//;s/[ \t]*$//' /tmp/new_4.txt | paste -s -d ',' | sed 's/,,/\n/g' | sed 's/^[,]*//;s/[,]*$//' > /tmp/new_5.txt
rm /tmp/new_4.txt
sed 's/Acct-Status-Type = //' /tmp/new_5.txt | sed 's/User-Name = //' |sed 's/Event-Timestamp = //' | sed 's/Acct-Terminate-Cause = //' | sed 's/Framed-IP-Address = //' | sed 's/Called-Station-Id = //' > /tmp/new_6.txt
rm /tmp/new_5.txt
awk -F"," '{ print $3 "," $1 "," $2 "," $6 "," $5 "," $4}' /tmp/new_6.txt | sed -e 's/"//g' | sed -e 's/,,/,/g' > /tmp/log.txt
rm /tmp/new_6.txt
Getting rid of all that disk IO will help a lot:
nice -20 sh <<'END_SCRIPT' > /tmp/log.txt
zcat /var/detail-${date}.gz |
sed '/./{H;$!d;};x;/46.241.178.96/!d' |
sed -n '/Acct-Status-Type/,/Called-Station-Id/p' |
sed '/Acct-Status-Type/{x;p;x;}' |
grep -Ev '(Acct-Authentic|Acct-Input-Octets|Acct-Input-Gigawords|Acct-Output-Octets|Acct-Output-Gigawords|Acct-Input-Packets|Acct-Output-Packets|Acct-Session-Time)' |
sed 's/^[ \t]*//;s/[ \t]*$//' |
paste -s -d ',' |
sed -e 's/,,/\n/g' \
-e 's/^[,]*//;s/[,]*$//' \
-e 's/Acct-Status-Type = //' \
-e 's/User-Name = //' \
-e 's/Event-Timestamp = //' \
-e 's/Acct-Terminate-Cause = //' \
-e 's/Framed-IP-Address = //' \
-e 's/Called-Station-Id = //' |
awk -F"," '{ print $3 "," $1 "," $2 "," $6 "," $5 "," $4}' |
sed -e 's/"//g' |
sed -e 's/,,/,/g'
END_SCRIPT
All the commands except zcat can be consolidated into a single awk or perl script: I don't have the time to help you with that right now.

perl - Extract data using grep and sed

I'm using this code to get all titles from urls with http://something.txt:
#!/usr/bin/perl -w
$output = `cat source.html | grep -o '<a .*href=.*>' | grep -E 'txt' | sed -e 's/<a /\n<a /g' | sed -e 's/<a .*title="//' | cut -f1 -d '"'`;
print("$output");
When i run this on perl i get the error:
sed: -e expression #1, char 6: unterminated `s' command
The error is related with this portion of code:
sed -e 's/<a /\n<a /g'
In backquotes, Perl uses the same rules as in double quotes. Therefore, \n corresponds to a newline; you have to backslash the backslash to pass literal \ to the shell:
`sed -e 's/<a /\\n<a /g'`

get list of sections from ini-file using shell (sed/awk)

I want to create a var from the section names of an ini file like:
[foo]
; ...
[bar]
; ...
[baz:bar]
;...
now I need a var like
SECTIONS="foo bar baz"
thanks in advance
One line solution could be:
export SECTIONS=`grep "^\[" test.ini |sort -u | xargs | tr '\[' ' ' | tr '\]' ' ' `
SECTIONS=$(crudini --get your.ini | sed 's/:.*//')
I'm now using this construct, don't need to know if a section exists. just read it, if it's empty it does not exist.
INI_FILE=test.ini
function ini_get
{
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
-e 's/;.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
< $INI_FILE \
| sed -n -e "/^\[$1\]/,/^\s*\[/{/^[^;].*\=.*/p;}"
echo ${!2}
}
IP=$(ini_get 50001 ip)
PORT=$(ini_get 50001 port)
echo $IP:$PORT