bash more effective use of sed / awk - bash

I am looking for a more efficient to use the following string to get the desired result as a one liner
date -d #1381219358 | sed 's/\ \ /\ /g' | sed 's/[:\ ]/-/g' | sed 's/2013/13/' | awk -F '-' '{print $4"-"$5"-"$6"-"$2"-"$3"-"$8}'
The desired result output is as follows:
04-02-38-Oct-8-13
Any help would be appreciated

You can format the output directly, like this:
date -d #1381219358 +"%H-%M-%S-%b-%d-%y"
10-02-38-Oct-08-13

I'm not sure if you need sed or awk for this. You can format the output using date.
Try saying:
date -d #1381219358 +%H-%M-%S-%b-%d-%y

Related

Combine multiple text files (row wise) into columns

I have multiple text files that I want to merge columnwise.
For example:
File 1
0.698501 -0.0747351 0.122993 -2.13516
File 2
-5.27203 -3.5916 -0.871368 1.53945
I want the output file to be like:
0.698501, -5.27203
-0.0747351, -3.5916
0.122993, -0.871368
-2.13516, 1.53945
Is there a one line bash common that can accomplish this?
I'll appreciate any help.
---Lyndz
With awk:
awk '{if(NR==1) {split($0,a1," ")} else {split($0,a2," ")}} END{for(i in a2) print a1[i] ", " a2[i]}' file1 file2
Output:
0.698501, -5.27203
-0.0747351, -3.5916
0.122993, -0.871368
-2.13516, 1.53945
paste <(cat file1 | sed -E 's/ +/&,\n/g') <(cat file2 | sed -E 's/ +/&\n/g') | column -s $',' -t | sed -E 's/\s+/, /g' | sed -E 's/, $//g'
It got a bit complicated, but I guess it can be done in a bit simpler way also.
P.S: Please lookup for the man pages of each command to see what they do.

Remove everything before a string with bash?

I'm doing this with ffmpeg :
ffmpeg -i /Users/petaire/GDrive/Taff/ASI/Bash/testFolder/SilenceAndBlack.mp4 -af silencedetect=d=2 -f null - 2>&1 | grep silence_duration
And my output is :
[silencedetect # 0x7f9e6940eba0] silence_end: 25.92 | silence_duration: 25.936
But I only want to keep the duration number, so I'm trying to remove everything before the last number.
I've never understood anything about sed/awk & co, so I dont know what is the best way to do that. I thought grep would be powerful enough, but it doesn't seems so.
Any idea?
Using awk to print the last field:
$ awk '{print $NF}'
Test it:
$ echo "[silencedetect # 0x7f9e6940eba0] silence_end: 25.92 | silence_duration: 25.936"| awk '{print $NF}'
25.936
or use sed to replace everything up to last space with nothing:
$ ... | sed 's/.* //'
you can change your grep command to
grep -oP '(?<=silence_duration: )\S+'
which will print the next field to the searched one.
to remove everything before the last number
you can use
grep -o "[^ ]*$"
Another option, grep -o with cut:
$ echo '[silencedetect # 0x7f9e6940eba0] silence_end: 25.92 | silence_duration: 25.936' \
| grep -o 'silence_duration: [0-9]*\.[0-9]*' | cut -d ' ' -f 2
25.936

Create name/value pairs based on file output

I'd like to format the output of cat myFile.txt in the form of:
app1=19
app2=7
app3=20
app4=19
Using some combination of piping output through various commands.
What would be easiest way to achieve this?
I've tried using cut -f2 but this does not change the output, which is odd.
Here is the basic command/file output:
[user#hostname ~]$ cat myFile.txt
1402483560882 app1 19
1402483560882 app2 7
1402483560882 app3 20
1402483560882 app4 19
Basing from your input:
awk '{ print $2 "=" $3 }' myFile
Output
app1=19
app2=7
app3=20
app4=19
Another solution, using sed and cut:
cat myFile.txt | sed 's/ \+/=/gp' | cut -f 3- -d '='
Or using tr and cut:
cat myFile.txt | tr -s ' ' '=' | cut -f 3- -d '='
You could try this sed oneliner also,
$ sed 's/^\s*[^ ]*\s\([^ ]*\)\s*\(.*\)$/\1=\2/g' file
app1=19
app2=7
app3=20
app4=19

bash scripting removing optional <Integer><colon> prefix

I have a list with all of the content is like:
1:NetworkManager-0.9.9.0-28.git20131003.fc20.x86_64
avahi-0.6.31-21.fc20.x86_64
2:irqbalance-1.0.7-1.fc20.x86_64
abrt-addon-kerneloops-2.1.12-2.fc20.x86_64
mdadm-3.3-4.fc20.x86_64
I need to remove the N: but leave the rest of strings as is.
Have tried:
cat service-rpmu.list | sed -ne "s/#[#:]\+://p" > end.list
cat service-rpmu.list | egrep -o '#[#:]+' > end.list
both result in an empty end.list
//* the N:, just denotes an epoch version */
With sed:
sed 's/^[0-9]\+://' your.file
Output:
NetworkManager-0.9.9.0-28.git20131003.fc20.x86_64
avahi-0.6.31-21.fc20.x86_64
irqbalance-1.0.7-1.fc20.x86_64
abrt-addon-kerneloops-2.1.12-2.fc20.x86_64
mdadm-3.3-4.fc20.x86_64
Btw, your list looks like the output of a grep command with the option -n. If this is true, then omit the -n option there. Also it is likely that your whole task can be done with a single sed command.
awk -F: '{ sub(/^.*:/,""); print}' sample
Here is another way with awk:
awk -F: '{print $NF}’ service-rpmu.list

changing the date format in shell script

I have a string and I need to separate the country name and date.
# echo 'india16-Feb-2013-20-33.sql' | sed 's/[0-9][0-9]//' | awk -F"-" '{print $1}'
india
# echo 'india16-Feb-2013-20-33.sql' | sed 's/india//' | awk -F"." '{print $1}'
16-Feb-2013-20-33
1) Is the above sed + awk correct? Is there any better way?
2) How do I format the date as 2013-02-16 20:33:00
I got the string (16-Feb) mentioned above using the following command. But I do not know how to change it back.
date '+%d-%b-%Y-%H-%M'
I couldn't get the sed with date versions working on osx so I did a Python version.
import datetime
import re
datestr = 'india16-Feb-2013-20-33.sql'
(country, date) = re.findall('(.*?)(\d{2}-.{3}-\d{4}-\d{2}-\d{2}).*', datestr)[0]
dt = datetime.datetime.strptime(date, "%d-%b-%Y-%H-%M")
print "country=", country
print "dt=", dt
Gives:
country= india
dt= 2013-02-16 20:33:00
I am starting to love sed
$ cat a.txt
india16-Feb-2013-20-33.sql
$ sed 's/[0-9].*//' a.txt
india
$ sed 's/[a-z]*//; s/-/ /3; s/-/:/3; s/.sql//' a.txt | xargs -0 date '+%F %T' -d
2013-02-16 20:33:00
Using sed and a single substitution:
sed 's/.*\([0-9]\{2\}-[A-Z][a-z]\{2\}-[0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\).*/date -d "\1 \2\3" +"%F %T"/ge'
Personally, I find it's a little bit easier on the eyes using GNU sed:
sed -r 's/.*([0-9]{2}-[A-Z][a-z]{2}-[0-9]{4})-([0-9]{2})-([0-9]{2}).*/date -d "\1 \2\3" +"%F %T"/ge'
Results:
2013-02-16 20:33:00

Resources