1

I have a file with a similar format to the following:

1.1.1.5 Ensure mounting of hfsplus filesystems is disabled
1.1.1.6 Ensure mounting of squashfs filesystems is disabled
1.1.15 Ensure nodev option set on /dev/shm partition
1.1.16 Ensure nosuid option set on /dev/shm partition
1.2.2 Ensure GPG keys are configured
1.3.1 Ensure AIDE is installed

They don't all start with Ensure.

I am trying to import this into a spreadsheet with the first column (the x.x.x.x) in the first column of the spreadsheet.

I need to delimit the first column with, for example, a comma so that LibreOffice can use it as a separator.

How can I change the file to add a comma after the first column so that LibreOffice can use it as a separator using bash?

6

You can use sed for this:

sed 's/ /,/' in > out

This will replace the first space on each line with a comma. If you want to modify the file in place, you can use the -i option. The syntax varies between different implementations of sed. For GNU sed, it's

sed 's/ /,/' -i the.file

For BSD sed, use

sed 's/ /,/' -i '' the.file
  • Welcome to Unix & Linux! Good first post. It's probably worth mentioning that the the -i, --inplace option is only available with GNU sed. – Anthony Geoghegan Jul 6 '17 at 10:48
  • 1
    @AnthonyGeoghegan Oh, I didn't know that since I never had to use any sed other than GNU. – NieDzejkob Jul 6 '17 at 10:49
  • 2
    @AnthonyGeoghegan that is not true... other implementations support -i too, but syntax varies... see unix.stackexchange.com/questions/92895/… and stackoverflow.com/questions/5694228/… – Sundeep Jul 6 '17 at 10:57
  • 1
    Many thanks for those links @Sundeep. I'm a GNU user and when I first started posting on SE sites, others commented that the -i wasn't portable. I could check the docs to see that -i is not specified by POSIX but without a BSD-like OS, I couldn't easily test option compatibility. Those links clarify the issue nicely. Thanks again. – Anthony Geoghegan Jul 6 '17 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.