Reputation: 213
[Bash Scripting] - I'm working with an LG device and I'm trying to obtain a concatenated Device ID string. I've figured out how to concatenate strings, but I'm having trouble extracting the strings from the overall output. From the bootloader, I run fastboot oem device-id and get an output like this. Everything after the "|" was actually outputed, I only included the "# | " as line references.
1 | ...
2 | (bootloader) ----------------------------------
3 | (bootloader) Device-ID
4 | (bootloader) ABCDEFGH12345678ABCDEFGH12345678
5 | (bootloader) 87654321HGFEDCBA87654321HGFEDCBA
6 | (bootloader) ----------------------------------
7 | OKAY [ 0.070s]
8 | finished. total time: 0.070s
Seems like fastboot oem device-id is a little restricted. Using grep with the command only displayed the same content. (fastboot oem device-id | grep bootloader -m 2) This should only display the first 2 lines (example). I also tried to output all the content to an external text file (and work from there), but it created a text blank file. (fastboot oem device-id >> test.txt)
Ideally, I would like to extract the contents of Lines 4 and 5, and store them into separate string variables - stringA and stringB, respectively. Strings A and B should output the following
(stringA) <-- "(bootloader) ABCDEFGH12345678ABCDEFGH12345678" (Line 4)
(stringB) <-- "(bootloader) 87654321HGFEDCBA87654321HGFEDCBA" (Line 5)
Then I would concatenate them like this.
echo "Device ID: $(stringA:13:32)$(stringB:13:32)"
Starting at column 13 (above) would exclude the (bootloader)_ part and leave only the desired string. In the end, the output of the echo (above) should look like this.
Device ID: ABCDEFGH12345678ABCDEFGH1234567887654321HGFEDCBA87654321HGFEDCBA
Strangely, I was able to do all of the above using a different fastboot command (fastboot devices). What other ways are there to extract the strings from Lines 4 and 5 (above) and store them into two separate string variables?
Upvotes: 1
Views: 614
Reputation: 113844
$ fastboot oem device-id | awk 'NR==4{a=$2} NR==5{b=$2; printf "Device ID: %s%s\n",a,b; exit}'
Device ID: ABCDEFGH12345678ABCDEFGH1234567887654321HGFEDCBA87654321HGFEDCBA
How it works:
NR==4{a=$2}
When we get to the fourth line, save the second field in the variable a
.
NR==5{b=$2; printf "Device ID: %s%s\n",a,b; exit}
When we get to the fifth line, save the second field in variable b
and write the output line.
$ fastboot oem device-id | sed -n 's/.* //; 4h; 5{H; x; s/\n//; s/^/Device ID: /p}'
Device ID: ABCDEFGH12345678ABCDEFGH1234567887654321HGFEDCBA87654321HGFEDCBA
How it works:
-n
Tell sed not to print anything unless we explicitly ask it to.
s/.* //
Find all characters up to and including the last space on the line and delete them.
`4h
If we are on the fourth line, save what's left to the hold space.
5{H; x; s/\n//; s/^/Device ID: /p}
If we are on the fifth line, append what's left of the line to the hold space (H
). Swap the hold space with the pattern space (x
). Remove the newline any newline character (s/\n//
). Add the string Device ID:
to the beginning of the pattern space and print it (s/^/Device ID: /p
).
$ fastboot oem device-id | grep -oE '[[:alnum:]]{32}$' | { read a; read b; echo "Device ID: $a$b"; }
Device ID: ABCDEFGH12345678ABCDEFGH1234567887654321HGFEDCBA87654321HGFEDCBA
How it works:
grep -oE '[[:alnum:]]{32}$'
If the line from fastboot ends with 32 alphanumeric characters, print those characters.
In more detail, the -o
option tells grep
to only print the matching part of a line. -E
tells grep to use extended regex. [[:alnum:]]{32}
matches 32 alphanumeric characters. $
matches only at the end of the line.
read a; read b; echo "Device ID: $a$b";
The first output line from grep
is read into variable a
. The second output line is read into variable b
. Then, the desired line is echoed to standard output.
Upvotes: 2
Reputation: 1822
If you have the output in a textfile output.txt
such as:
user@host:~$ cat output.txt
...
(bootloader) ----------------------------------
(bootloader) Device-ID
(bootloader) ABCDEFGH12345678ABCDEFGH12345678
(bootloader) 87654321HGFEDCBA87654321HGFEDCBA
(bootloader) ----------------------------------
OKAY [ 0.070s]
finished. total time: 0.070s
The following command:
cat output.txt | sed -n '4,5p' | awk '{print $2}' | tr -d '\n'
Thus it finally gives:
ABCDEFGH12345678ABCDEFGH1234567887654321HGFEDCBA87654321HGFEDCBA
Of course, this one works as well:
cat output.txt | sed -n '4,5p' | cut -d\ -f2 | tr -d '\n'
Upvotes: 0