Wednesday, December 29, 2021

[SOLVED] how to get values from file using sed,awk or grep on linux command/scripting?

Issue

i have file1 with value:

<action>
 <row>
    <column name="book" label="book">stick man (2020)/</column>
    <column name="referensi" label="referensi"> http://172.22.215.234/Data/Book/Journal/2016_2020/1%20Stick%20%282020%30/</column>
 </row>
<row>
    <column name="book" label="book">python easy (2019)/</column>
    <column name="referensi" label="referensi"> http://172.22.215.234/Data/Book/Journal/2016_2020/2%20Buck%20%282019%30/</column>
 </row>
</action>

i want to get the contents of the file using linux scripting or command (sed, grep or awk). example output:

stick man (2020) | http://172.22.215.234/Data/Book/Journal/2016_2020/1%/20Stick%20%282020%30
python easy (2019) | http://172.22.215.234/Data/Book/Journal/2016_2020/%2/20Buck%20%282019%30

my code:

grep -oP 'href="([^".]*)">([^</.]*)' file1

please help i am newbie :)


Solution

$ awk -v RS='<[^>]+>' 'NF{printf "%s", $0 (++c%2?" |":ORS)}' file

stick man (2020)/ | http://172.22.215.234/Data/Book/Journal/2016_2020/1%20Stick%20%282020%30/
python easy (2019)/ | http://172.22.215.234/Data/Book/Journal/2016_2020/2%20Buck%20%282019%30/

note that forward slashes are in your original data

requires multi-char RS support (GNU awk).



Answered By - karakfa