简体   繁体   中英

awk/sed on csv with #text

I am newbie here, so any directions would help. Have a csv file ( comma seprated ) and in one of the columns there is text delimited with # sign ( the column is delimeted by # on it`s own ). Here is an example of a line:

text,text devicetype,cidr,text#more text#XXXX|more
text#even more text#more
text#text_toreplace#text|text#text

text_toreplace is what i want substitued with different text, without changing all the rest of the data in the column with value "text#more text#XXXX|more text#even more text#more text#text_toreplace#text|text#text"

The CSV does have the first raw as headers and there would be multiple lines after that.

Sample output would be exactly the same with the coumn dlimted by # and one value differance:

text,text devicetype,cidr,text#more text#XXXX|more
text#even more text#more
text#REPLACED_TEXT#text|text#text

Ideas

Use # as the "primary" field separator, iterate over the fields to replace the offending text.

awk -F'#' -v OFS='#' '{
    for (i=1; i<=NF; i++)
        if ($i == "text_toreplace")
            $i = "REPLACED_TEXT"
    print
}' <<'END_INPUT'
text,text devicetype,cidr,text#more text#XXXX|more
text#even more text#more
text#text_toreplace#text|text#text
END_INPUT
text,text devicetype,cidr,text#more text#XXXX|more
text#even more text#more
text#REPLACED_TEXT#text|text#text

with sed it will be easier

$ sed -E 's/(^|#)(text_toreplace)(#|$)/\1REPLACED_TEXT\3/g' file

anchors required to match the first and the last field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM