Doubts in exercise how to change colums with awk or sed

Xremix31

New Member
Joined
Jan 28, 2022
Messages
4
Reaction score
2
Credits
36
I have this exercise, but i don't know that miss in my comand because not show only 5 columns that contained RAID.
Run a command on stdout as columns, 5, 4, 1, 3, 2 (in this order), separated by $ the file /usr/pci.ids but as lines that contained RAID word.
I think only use the command sed or awk in this exercicse.

I try this comand:
awk -F:"$" 'NR==5, /RAID/{print($5,$4,$1,$3,$2)}' /usr/share/misc/pci.ids but doesn't work correctly.
 


JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,492
Reaction score
2,116
Credits
10,643
You're close, but your syntax is a little off.
And awk is definitely the correct tool for this task.
Sed is for editing streams of text, whereas awk is more for breaking lines of text into fields and then reporting them.

I think this will work:
Bash:
awk -F'$' 'NR==5; /RAID/ {print $5, $4, $1, $2, $3}' /usr/share/misc/pci.ids
 
OP
X

Xremix31

New Member
Joined
Jan 28, 2022
Messages
4
Reaction score
2
Credits
36
You're close, but your syntax is a little off.
And awk is definitely the correct tool for this task.
Sed is for editing streams of text, whereas awk is more for breaking lines of text into fields and then reporting them.

I think this will work:
Bash:
awk -F'$' 'NR==5; /RAID/ {print $5, $4, $1, $2, $3}' /usr/share/misc/pci.ids
Hi, thanks for try help me. Your code is work but not chanche the order of columns and not put the '$' for separate.
 

smooth_buddha

Active Member
Joined
Feb 13, 2020
Messages
362
Reaction score
244
Credits
1,648
I have this exercise, but i don't know that miss in my comand because not show only 5 columns that contained RAID.
Run a command on stdout as columns, 5, 4, 1, 3, 2 (in this order), separated by $ the file /usr/pci.ids but as lines that contained RAID word.
I think only use the command sed or awk in this exercicse.

I try this comand:
awk -F:"$" 'NR==5, /RAID/{print($5,$4,$1,$3,$2)}' /usr/share/misc/pci.ids but doesn't work correctly.
may i ask are you following a book or course as you mention this is an exercise?? would be interested to know where the exercise is from?
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,492
Reaction score
2,116
Credits
10,643
Hi, thanks for try help me. Your code is work but not chanche the order of columns and not put the '$' for separate.
It does change the order of the columns.
The print statement in awk displays column 5, 4, 1, 2, 3

And where you were using -F'$' - That tells awk that '$' is the separator between the fields in the original/input file..
Which means the original file would have to contain entries that looked like this:
Code:
        1000$9460$MegaRAID$9460-16i

Note: I was on my phone before and was just going by the information you included in your post. So I assumed you were using -F correctly.

But I've just fired up my laptop and taken a look in /usr/share/misc/pci.ids and it contains entries like this:
Code:
    0680  Ultra ATA/133 IDE RAID CONTROLLER CARD
    ae10  Smart-2/P RAID Controller
        0e11 4048  RAID LC2 Controller
    0014  MegaRAID Tri-Mode SAS3516
        1000 9460  MegaRAID 9460-16i
        1000 9480  MegaRAID 9480-8i8e
        1000 9481  MegaRAID 9480-8e
        1137 020e  UCSC-RAID-M5 12G Modular RAID Controller
        1d49 0602  ThinkSystem RAID 930-16i 4GB Flash PCIe 12Gb Adapter
        1d49 0604  ThinkSystem RAID 930-8e 4GB Flash PCIe 12Gb Adapter
        1d49 0607  ThinkSystem RAID 930-16i 8GB Flash PCIe 12Gb Adapter
        8086 352d  Integrated RAID Module RMSP3AD160F
        8086 9460  RAID Controller RSP3TD160F
        8086 9480  RAID Controller RSP3MD088F
    0015  MegaRAID Tri-Mode SAS3416
        1000 9441  MegaRAID 9440-16i
There are no '$' characters in the file. The field specifier that you were using with awk tells awk which character separates the fields/records on each line of the input file.
So you shouldn't be using the -F'$' flag with awk.
If you do, it will count the entire file as a single record, or each line as a single record. I'm not sure which. But either way you'll end up just seeing everything in the file.
Regarding the '$' - I'm confused now... Perhaps there is something that is being lost in translation somewhere?!

Anyway - after having a play, this looks like it will be more correct:
Bash:
awk 'BEGIN {NR==5} /RAID/ {print $5, $4, $1, $2, $3}' pci.ids

That will separate the records in the file on spaces and should only work with lines containing exactly 5 records AND containing the string "RAID".
The and the output is ordered field 5, 4, 1, 2, 3.

So if I run the above command on a file containing the lines I've posted above, I'd get the following results:
Code:
RAID IDE 0680 Ultra ATA/133
 Controller ae10 Smart-2/P RAID
Controller LC2 0e11 4048 RAID
 SAS3516 0014 MegaRAID Tri-Mode
 9460-16i 1000 9460 MegaRAID
 9480-8i8e 1000 9480 MegaRAID
 9480-8e 1000 9481 MegaRAID
Modular 12G 1137 020e UCSC-RAID-M5
930-16i RAID 1d49 0602 ThinkSystem
930-8e RAID 1d49 0604 ThinkSystem
930-16i RAID 1d49 0607 ThinkSystem
Module RAID 8086 352d Integrated
RSP3TD160F Controller 8086 9460 RAID
RSP3MD088F Controller 8086 9480 RAID
 SAS3416 0015 MegaRAID Tri-Mode
 9440-16i 1000 9441 MegaRAID

Going back to the '$' field separator - do you want the field separator to appear in the output?
If so, you need to do this:
Bash:
awk 'BEGIN {NR==5; OFS="$"} /RAID/ {print $5, $4, $1, $2, $3}' pci.ids
Then '$' will be used as the separator in the output.
So if I run the above command on the lines I've posted from the file, we'd get this:
Code:
RAID$IDE$0680$Ultra$ATA/133
$Controller$ae10$Smart-2/P$RAID
Controller$LC2$0e11$4048$RAID
$SAS3516$0014$MegaRAID$Tri-Mode
$9460-16i$1000$9460$MegaRAID
$9480-8i8e$1000$9480$MegaRAID
$9480-8e$1000$9481$MegaRAID
Modular$12G$1137$020e$UCSC-RAID-M5
930-16i$RAID$1d49$0602$ThinkSystem
930-8e$RAID$1d49$0604$ThinkSystem
930-16i$RAID$1d49$0607$ThinkSystem
Module$RAID$8086$352d$Integrated
RSP3TD160F$Controller$8086$9460$RAID
RSP3MD088F$Controller$8086$9480$RAID
$SAS3416$0015$MegaRAID$Tri-Mode
$9440-16i$1000$9441$MegaRAID
Is that what you're looking for?
 
Last edited:
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top