I'm trying to replace a string using AWK pipe out to SED
grep pdo_user /html/data/_default_/configs/application.ini | awk '{print $3}' | sed -i 's/$1/"username"/g' /html/data/_default_/configs/application.ini
but found string is not replaced
Output for
grep pdo_user /html/data/_default_/configs/application.ini | awk '{print $3}'
is
"root"
Any tips on that?
I suggest to use awk and mv:
awk '/pdo_user/ && $3=="\"root\"" {$3="\"username\""}1' /path/to/application.ini > /path/to/application.tmp
mv /path/to/application.tmp /path/to/application.ini
Working solution based on Shelter's tip using AWK and SED
sed -i 's/'$(awk '/pdo_user/{print $3}' /path/to/application.ini)'/"username"/' /path/to/application.ini
Related
I have string lists :
./SolutionController.php core/app/Http/Controllers/Admin/SolutionController.php
./ContentController.php core/app/Http/Controllers/Frontpage/ContentController.php
./country-flag vendor/country-flag
I wish I could get the final value between the './' sign and the 'space'
Output:
SolutionController.php
ContentController.php
country-flag
This code with bash script:
#!/bin/bash
tanggal=$(date +%d-%m-%Y)
filename="./update/$tanggal/lists.md"
n=1
tanggalWaktu=$(date +"%d-%m-%Y %H:%M:%S")
mkdir -p ./logs
while read line; do
fileName=$(awk -F'[/ ]' '{print $2}' $line)
echo "file -> $fileName"
done < $filename
Output:
awk: can't open file ./SolutionController.php
source line number
Please help me
Using awk :
awk -F'[/ ]' '{print $2}' string.txt
Using gawk:
awk '{print gensub(/\.\/(.*) (.*)/,"\\1","g")}' string.txt
Test Results:
$ cat string.txt
./SolutionController.php core/app/Http/Controllers/Admin/SolutionController.php
./ContentController.php core/app/Http/Controllers/Frontpage/ContentController.php
./country-flag vendor/country-flag
$ awk -F'[/ ]' '{print $2}' string.txt
SolutionController.php
ContentController.php
country-flag
$ awk '{print gensub(/\.\/(.*) (.*)/,"\\1","g")}' string.txt
SolutionController.php
ContentController.php
country-flag
You can do it
echo "./SolutionController.php core/app/Http/Controllers/Admin/SolutionController.php" | sed -r 's/\.\/(.*) .*/\1/'
If you stored it in the file.
sed -r 's/\.\/(.*) .*/\1/' strings.txt
I have some text I need to split up to extract the relevant argument, and my [g]awk match command does not behave - I just want to understand why?! (I have written a less elegant way around it now...).
So the string is blahblah|msgcontent1=HeaderUUIiewConsenFlagPSMessage|msgtype2=Blah002|msgcontent2=header
I want to output just the contents of msgcontent1=, so did
echo "blahblah|msgcontent1=HeaderUUIiewConsenFlagPSMessage|msgtype2=Blah002|msgcontent2=header" | gawk '{ if (match($0,/msgcontent1=(.*)[|]/,a)) { print a[1]; } }'
Trouble instead of getting
HeaderUUIiewConsenFlagPSMessage
I get the match with everything from there to the last pipe of the string HeaderUUIiewConsenFlagPSMessage|msgtype2=Blah002
Now I accept this is because the regexp in /msgcontent1=(.*)[|]/ can match multiple ways, but HOW do I make it match the way I want it to??
With your shown samples please try following. Written and tested in GNU awk this will print only contents from msgcontent1= till | first occurrence.
awk 'match($0,/msgcontent1=[^|]*/){print substr($0,RSTART+12,RLENGTH-12)}' Input_file
OR with echo + awk try:
echo "blahblah|msgcontent1=HeaderUUIiewConsenFlagPSMessage|msgtype2=Blah002|msgcontent2=header" |
awk 'match($0,/msgcontent1=[^|]*/){print substr($0,RSTART+12,RLENGTH-12)}'
With FPAT option in GNU awk:
awk -v FPAT='msgcontent1=[^|]*' '{sub(/.*=/,"",$1);print $1}' Input_file
This is your input:
s='blahblah|msgcontent1=HeaderUUIiewConsenFlagPSMessage|msgtype2=Blah002|msgcontent2=header'
You may use gnu awk like this to extract value after msgcontent1=:
awk -F= -v RS='|' '$1 == "msgcontent1" {print $2}' <<< "$s"
HeaderUUIiewConsenFlagPSMessage
or using this sed:
sed -E 's/^(.*\|)?msgcontent1=([^|]+).*/\2/' <<< "$s"
HeaderUUIiewConsenFlagPSMessage
Or using this gnu grep:
grep -oP '(^|\|)msgcontent1=\K[^|]+' <<< "$s"
HeaderUUIiewConsenFlagPSMessage
echo "blahblah|msgcontent1=HeaderUUIiewConsenFlagPSMessage|msgtype2=Blah002|msgcontent2=header" | awk '{ if (match($0,/msgcontent1=([^\|]*)/,a)) print a[1] }'
this prints HeaderUUIiewConsenFlagPSMessage
The reason your regex match msgcontent1=HeaderUUIiewConsenFlagPSMessage|msgtype2=Blah002 is that matching is 'hungry' so it allways finds the longest possible match
Also with awk:
echo 'blahblah|msgcontent1=HeaderUUIiewConsenFlagPSMessage|msgtype2=Blah002|msgcontent2=header' | awk -v FS='[=|]' '$2 == "msgcontent1" {print $3}'
HeaderUUIiewConsenFlagPSMessage
I would like to grab the part after "-" and combine it with the following letter-string into a tab-output. I tried something like cut -d "*-" -f 2 <<< "$your_str" but I am not sure how to do the whole shuffling.
Input:
>1-395652
TATTGCACTTGTCCCGGCCTGT
>2-369990
TATTGCACTCGTCCCGGCCTCC
>3-132234
TATTGCACTCGTCCCGGCCTC
>4-122014
TATTGCACTTGTCCCGGCCTGTAA
>5-118616
Output:
TATTGCACTTGTCCCGGCCTGT 395652
TATTGCACTCGTCCCGGCCTCC 369990
awk to the rescue!
awk -F- '/^>/{k=$2; next} {print $0, k}' file
With GNU sed:
sed -nE 'N;s/.*-([0-9]+)\n(.*)/\2\t\1/p' file
Output:
TATTGCACTTGTCCCGGCCTGT 395652
TATTGCACTCGTCCCGGCCTCC 369990
TATTGCACTCGTCCCGGCCTC 132234
TATTGCACTTGTCCCGGCCTGTAA 122014
Portable sed:
sed -n 's/.*-//;x;n;G;s/\n/ /p' inputfile
Output:
TATTGCACTTGTCCCGGCCTGT 395652
TATTGCACTCGTCCCGGCCTCC 369990
TATTGCACTCGTCCCGGCCTC 132234
TATTGCACTTGTCCCGGCCTGTAA 122014
[QFJ Timer]:2014-07-02 06:19:09,030:bla.all.com.bla.bla.ppp.xxx.abcsedf:
i would like to extract the date and time.
so the date is no problem :
cat bla.log |awk -F: '{print $2}'|awk '{print $1}'
now the issue is with the time.
if i do : cat bla.log |awk '{print $3}' so i get:
06:19:09,030:bla.all.com.bla.bla.ppp.xxx.abcsedf:
which mean that i need another grep here right?
but i did so many tries using also 'FS' and didn't get only the time.
Can someone please advise?
Thank you.
In the GNU version of awk FS can be a regexp:
echo "[QFJ Timer]:2014-07-02 06:19:09,030:bla.all.com.bla.bla.ppp.xxx.abcsedf:" |
awk -vFS=":|," '{ print $2":"$3":"$4;}'
which spits out
2014-07-02 06:19:09
Your left separator is ':' and the right is ',', and unfortunately hours, minutes and seconds are also separated by your left separator. That is solved by printing $3 and $4. Quick and dirty solution, but it isn't going to be be very robust.
You could use sed for this purpose,
$ echo '[QFJ Timer]:2014-07-02 06:19:09,030:bla.all.com.bla.bla.ppp.xxx.abcsedf:' | sed 's/^[^:]*:\([^,]*\).*/\1/g'
2014-07-02 06:19:09
cat bla.log |awk -F":" '{print $2":"$3":"$4}' | awk -F"," '{print $1}'
Which gets you:
2014-07-02 06:19:09
You can use grep, since it is meant for that:
grep -o '[0-9]\{4\}\(-[0-9]\{2\}\)\{2\}\(\( \|:\)[0-9]\{2\}\)\{3\}' log.file
or, a little bit simpler, egrep:
egrep -o '[0-9]{4}(-[0-9]{2}){2}(( |:)[0-9]{2}){3}' log.file
The following works as expected.
awk -F'^' '{printf "set %s:%s %s\n",$1,$2, $7}' todel.txt | sed 's/$/\\r\\n/' >> tofile.txt
But when I try to add a variable to output as shown below, I get an error:
awk -F'^' '{printf "set %s:%s:%s %s\n",$1,$2,$myvar $7}' todel.txt | sed 's/$/\\r\\n/' >> tofile.txt
$myvar doesn't expand in single quote.
You can use the -v option to pass shell variable to awk:
awk -F'^' -v myvar=$myvar '{printf "set %s:%s:%s %s\n",$1,$2,myvar,$7}' todel.txt