An answer to another question suggests sed -i 's/original/replacement/g' file.txt
to replace specific words in a text file. My starting situation looks like this:
Item: PRF
Type: File
Item: AOX
Type: Folder
Item: DD4
Type: File
My ending situation should look like this:
Item: PRF^Type: File
Item: AOX^Type: Folder
Item: DD4^Type: File
Notes: (1) The Ask Ubuntu interface seems to suppress some of the leading spaces before Item: and Type:. There are in fact eight leading spaces. (2) I may have erred in using simplistic examples of Item. The items are actually partial Windows paths (lacking e.g., D:), some of which are quite long. A more accurate example would be Item: FolderSome FolderA file name.txt.
I've tried this, with and without double quotes:
sed -i 's/
" Type: "/^"Type: "/g' file.txt
That gives me no errors, but also no changes. Also tried this:
awk '/ " Item: " / { printf "%s", $0"^" } / " Type: " / { gsub(/^[ ]+/,"",$0); print $0 }' source.txt
I tried that to verify that I would be changing only those entries with eight blank spaces before "Item." That didn't work. Trying it with no spaces and no double quotes, as in the answer (below), also failed. Trying it with gawk -i inplace
produced source.txt containing zero bytes.
My title initially specified sed
. An answer proposing awk
alerted me to that alternative, which (now that I'm looking at it) seems more capable. But I cannot figure out how to make it work.