简体   繁体   中英

How to add a part of a line (for every line in a file) at the end of the line

I have stored coordinates in a table like this:

140.7 44.9,140.7 44.7,141.0 44.7,141.1 44.8
...

How can I copy the first to values 140.7 44.9 (plus one comma) to the end of the line so that this will be a closed polygon?

140.7 44.9,140.7 44.7,141.0 44.7,141.1 44.8, 140.7 44.9

Can this be achieved in one command, or do I have to grep the first part and then append this to the other lines?

how about this awk one-liner:

awk -F, -v OFS="," '{print $0,$1}' file

change into OFS=", " if you love that whithspace after the last comma.

Using sed :

s='140.7 44.9,140.7 44.7,141.0 44.7,141.1 44.8'
sed -r 's/([^,]+)(.*)/\0,\1/' <<< "$s"
140.7 44.9,140.7 44.7,141.0 44.7,141.1 44.8, 140.7 44.9

Using awk:

awk -F, -v OFS=', ' '{print $0, $1}' <<< "$s"
140.7 44.9,140.7 44.7,141.0 44.7,141.1 44.8, 140.7 44.9

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