Using sed to modify command output
The output of a command changed and I need to extract the data and print it out in a different fassion:
Code:
abcd1=aaaa xx Code:
output 1 = xx How is this done with sed? Can someone please enlighten me? |
I think we need to see a bit more of the context here, because I'm not exactly sure what you're trying to accomplish. For example, does this command output one line at a time, so that they can be filtered individually, or a block of lines that needs to be formatted all at once? Are the "output" and "item" in the desired output literal strings, or something that's fetched from the input? You mention searching for substrings, but I'm not sure what you mean by that here. A real world example might be more helpful.
Offhand, from the examples given, I imagine awk might be a better tool to use here than sed, since the input can be read as a series of separate fields. |
I have made a script for this purpose.
Code:
cat data | grep = | sed 's/abcd/output /' | awk -F"=" '{print $1}' | sed 's/.*/& =/' > file1 This might not be the best script, but will at least serve your purpose. Here, "data" is your raw file. Afterm running this script, it will give you two files final1 and final2. Code:
[linux1@HMLINUX1 abc]$ cat final1 Code:
output 1 = xx |
Since RHEL 5.3, the SAN WWNs could only be retrieved via the sys filesystem in the following fashion:
Code:
# (systool -c fc_host -v && systool -c fc_remote_ports -v) | grep -E '\b+Class Device path = "/sys/class/fc_host/host[0-9]+"$|\b+node_name.*= "0x[0-9a-f]{16}"$|\b+Class Device path = "/sys/class/fc_remote_ports/rport-[0-9+:[0-9]+-[0-9]+"' Code:
scsi-qla0-adapter-node=50060b0000c28e05; e.g.: 'host0' should be rewritten to 'scsi-qla0-adapter-node' and its wwn should be extracted from the next line ' node_name = "0x50060b0000c28e05"' so that it looks like '50060b0000c28e05'. Together: 'scsi-qla0-adapter-node=50060b0000c28e05;'. The scsi-qla{0,1}-target-0 and scsi-qla{0,1}-target-1 are the uniqed wwn values of the rport Device paths. rport-0 meaning, that it is connected to qla0 and 'rport-1' means that it is connected to qla1. Certainly this would be an easy task by writing a complete script that transforms the output. Even easier when using a higher level language such as Python or Perl. But I don't want to install a script on the target systems and I have no guarantee, that Perl or Python would be available on all systems. Therefor, I want to use simple bash for extracting the info via SSH command. I think that this is very doable with a sed command | sort | uniq. But I need a little help how to first search for a line, then find a substring in that line and rewriting the resulting output to the old format of pre RHEL 5.3. |
Dear Brian,
I would recommend you not to post a new query in the same thread. Has your previous problem resolved ? |
I'm sorry that my posting was confusing. Actually it is the very same problem of reformatting data.
The real world problem is more complex, though. My current solution is as follows: Code:
(systool -c fc_host -v && systool -c fc_remote_ports -v) | grep -E '\b+Class Device path = "/sys/class/fc_host/host[0-9]+"$|\b+node_name.*= "0x[0-9a-f]{16}"$|\b+Class Device path = "/sys/class/fc_remote_ports/rport-[0-9]+:[0-9]+-[0-9]+"' | sed -e 's#.*host\([0-9]*\).*"#scsi-qla\1-adapter-node=#' -e 's#.*rport-\([0-9]*\):[0-9]*-\([0-9]*\).*#scsi-qla\1-target-\2=#' -e '$!N;s#\n##' -e 's#=.*node_name.*= "0x#=#g' -e 's#"$#;#g' | sort Instead of Code:
scsi-qla1-target-0=500a098089fb3c2e; Code:
scsi-qla1-target-0=500a098089fb3c2e; Has anyone an idea how to shorten the sed commandline? |
So based on your input given above I came up with the following, let me know if it helps:
Code:
#!/usr/bin/awk -f |
Hello grail,
thank you for your time and input! This is indeed a very good solution! Code:
#(systool -c fc_host -v && systool -c fc_remote_ports -v) | grep -E '\b+Class Device path = "/sys/class/fc_host/host[0-9]+"$|\b+node_name.*= "0x[0-9a-f]{16}"$|\b+Class Device path = "/sys/class/fc_remote_ports/rport-[0-9]+:[0-9]+-[0-9]+"' | ./test.awk |
All times are GMT -5. The time now is 09:01 PM. |