awk help > Sending output to a command - openwrt

I am by no means a programmer. I'll put that right out there. However, I'm trying to write a script that writes fstab entries after grabbing uuid data. This is in a OpenWRT environment on my router. My goal:
Grab uuid info using awk, like:
blkid /dev/sdb2 | awk -F'UUID="|"' '{print $2}'
Send the full uuid output to the end of this command:
uci set fstab.#mount[-1].uuid=
Execute that command with the correct uuid.
This command writes that uuid to the correct place in fstab. How can this be accomplished in a bash-script?
Thanks,
KG

You can either execute the command from within awk (using system(...)), or from within the shell (using uci set fstab.#mount[-1].uuid=$(blkid ...)).

I can't test it with the uci command but I believe this should do what you need.
bblkid /dev/sda1 | uci set fstab.#mount[-1].uuid=$( awk -F'UUID="|"' '{print $2}' )
Edit: this would probably be better.
uci set fstab.#mount[-1].uuid=$( bblkid /dev/sda1 | awk -F'UUID="|"' '{print $2}' )
Edit 2: little bit shorter but should get the same result. Promise this is the last one
uci set fstab.#mount[-1].uuid=$( bblkid /dev/sda1 | tr -d UUID=\" )

The above options didn't work quite right.
What if I have this in fstab:
option uuid 'hotdog'
I want to replace the word "hotdog" with the output of the uuid from this command:
On my router that output looks like:
root#OpenWrt:/etc/config# blkid /dev/sdb2 | awk -F'UUID="|"' '{print $2}'
8618b8fe-93a9-488c-a901-0df30898c82e
or
root#OpenWrt:/etc/config# block info | grep sdb2
/dev/sdb2: UUID="8618b8fe-93a9-488c-a901-0df30898c82e" NAME="EXT_JOURNAL" VERSION="1.0" TYPE="ext4"
I want to then replace the word hotdog with the random uuid, and keep the semi-colons. Keep in mind that the uuid will change each time I format that partition.

Related

grep -o search stop at first instance of second expression, rather then last? Greedy?

Not sure who to phrase this question
This is an example line.
30/Oct/2019:00:17:22 +0000|v1|177.95.140.78|www.somewebsite.com|200|162512|-|-|0.000|GET /product/short-velvet-cloak-with-hood/?attribute_pa_color=dark-blue&attribute_pa_accent-color=gold&attribute_pa_size=small HTTP/1.0|0|0|-
I need to extract attribute_pa_color=
So I have
cat somewebsite.access.log.2.csv | grep -o "?.*=" > just-parameters.txt
Which works but if there are multiple parameters in the URL is returns all of them
So instead of stopping the match at the first instance of "=" its taking the last instance of "=" in the line.
How can I make it stop at the first.
I tried this
cat somewebsite.access.log.2.csv | grep -o "?(.*?)=" > just-parameters2.txt
cat somewebsite.access.log.2.csv | grep -o "\?(.*?)=" > just-parameters2.txt
Both return nothing
Also I need each unique parameter so once I created the file I ran
sort just-parameters.txt | uniq > clean.txt
Which does not appear to work, is it possible to remove duplicates and have it be part of them same command?
You can try something like with awk
awk -F'[?&]' '{print $2}' somewebsite.access.log.2.csv|sort -u > clean.txt
This will work if attribute_pa_color is the first parameter on URL
If you want to extract only text attribute_pa_color= you can try something like:
awk -F'[?&]' '{print $2}' somewebsite.access.log.2.csv|awk -F\= '{print $1"="}'|sort -u > clean.txt
Instead of using second awk you can try something like:
awk -F'[?&]' '{split($2,a,=);print a[1]}' somewebsite.access.log.2.csv|sort -u > clean.txt
Split internally in awk using = as delimiter

How do I grep the list of results and print them with serial number?

I used a command in Sqoop to list the tables in my SQL Server that start with 'lkp' in table names
$sqoop list-tables | grep -i 'lkp'
What I need is I want to list the 'lkp' tables with serial numbers so I tried with the command
$sqoop list-tables | grep -in 'lkp'
but it resulted with numbers of entire list of tables irrespective of name 'lkp' as
7:LKP_AttributeType
11:LKP_CalendarName
22.LKP_CategoryError
27:LKP_ColumnDataType
38:LKP_ColumnName
and so on....
what I need is
1:LKP_AttributeType
2:LKP_CalendarName
3.LKP_CategoryError
4:LKP_ColumnDataType
5:LKP_ColumnName
Can any one explain me how to achieve that ?
Not familiar with sqoop, but can you use the "nl" command line tool to add line numbers to the grep output?
$sqoop list-tables | grep -i 'lkp' | nl
try this command line
sqoop list-tables | grep -i 'lkp'| awk '{print NR,$0}'

Find parameters of a method with grep

I need some help with a grep command (in the Bash).
In my source files, I want to list all unique parameters of a function. Background: I want to search through all files, to see, which permissions ([perm("abc")] are used.
Example.txt:
if (x) perm("this"); else perm("that");
perm("what");
I'd like to have my grep output:
this
that
what
If I do my grep with this search expression
perm\(\"(.*?)\"\)
I'll get perm("this), perm("that"), etc. but I'd like to have just the permissions: this and that and what.
How can I do that?
Use a look-behind:
$ grep -Po '(?<=perm\(")[^"]*' file
this
that
what
This looks for all the text occurring after perm(" and until another " is found.
Note -P is used to allow this behaviour (it is a Perl regex) and -o to just print the matched item, instead of the whole line.
Here is a gnu awk version (due to multiple characters in RS)
awk -v RS='perm\\("' -F\" 'NR>1 {print $1}' file
this
that
what

Awk processing and parsing

Trying to use STRICTLY awk to solve a little issue and I can't wrap myself around the solution:
# more connections
0x828ac008 127.0.0.1:5152 127.0.0.1:1387 2000
0x82cc28f8 10.4.4.63:1435 10.4.4.72:22 1132
0x82ec1de0 10.4.4.63:1524 50.28.90.36:8080 3248
# awk -F":" '/[0-9]/{print $1,$2}' connections | awk '!/^127/{print "nslookup "$4}'
nslookup 127.0.0.1
nslookup 10.4.4.72
nslookup 50.28.90.36
I am looking for a streamlined method to just parse out anything aside from loopbacks ^127 and the 10. addresses in my netblock. Yes, I know I can use sed, grep, cut, etc, but I'm hoping to see how someone else would do this in awk. Its more of a learning curve/a-ha! thing
Clarifying: Output would omit 10.x.x.x and 127.x.x.x
Output would be
nslookup 50.28.90.36
I tried awk !/^127\.|^10\./ but I couldn't get it to ignore the values.
This awk should work:
awk -F "[: ]+" 'NR>1 && !($4 ~ /^(127|10)\./){print "nslookup", $4}' connections
OUTPUT:
nslookup 50.28.90.36
Anubhava offers a great solution by setting Field Separators. Here is an alternate way of doing it using the default FS:
Using split function:
awk '$3!~/^(127|10)/{split($3,ip,/:/);print "nslookup",ip[1]}' connections
Using sub function:
awk '$3!~/^(127|10)/{sub(/:.*/,"",$3);print "nslookup", $3}' connections
Also a bash way of doing it:
while read -ra lines; do
[[ ! ${lines[2]} =~ "^(127|10).*" ]] && echo "nslookup ${lines[2]%:*}";
done < connections

basic grep

I have a large file where each line contains a substring such as ABC123. If I execute
grep ABC file.txt
or
grep ABC1 file.txt
I get those lines back as expected, but if I execute
grep ABC12 file.txt
grep fails to find the corresponding lines.
This seems pretty trivial functionality, but I'm not a heavy user of grep so perhaps I'm missing some gotcha.
Use something like
od -x -a < filename
to dump out the file contents in hex. That'll immediately show you if what you have in your file is what you expect. Which I suspect it isn't :-)
Note: od has lots of useful options to help you here. Too many to list, in fact.
Is there a chance your file contains some hidden character, such as 0x00 ?
This doesn't make sense. Are you sure the file contains "ABC123"?
You can verify this by running following command in a shell
echo "ABC123" | grep ABC12
If the lines contain ABC123, then "grep ABC12" should get them. Do you perhaps mean that you want to match several different strings, such as ABC1, ABC2 and ABC3? In that case you can try this:
grep -E 'ABC1|ABC2|ABC3'
I'm not sure what the problem is.. grep works exactly as it should.. For example, the contents of my test file:
$ cat file.txt
ABC
ABC1
ABC12
ABC123
..and grep'ing for ABC, ABC1, ABC12, ABC123:
$ grep ABC file.txt
ABC
ABC1
ABC12
ABC123
$ grep ABC1 file.txt
ABC1
ABC12
ABC123
$ grep ABC12 file.txt
ABC12
ABC123
$ grep ABC123 file.txt
ABC123
grep is basically a filter, any line containing the first argument (ABC, or ABC1 etc) will be displayed. If it doesn't contain the entire string, it will not be displayed

Resources