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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 1
    looks like you want to replace first space with comma, if so check out sed command.. very simple to do it... https://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files – Sundeep Jul 06 '17 at 10:25

1 Answers1

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
Maya
  • 378
  • Welcome to [unix.se]! Good first post. It's probably worth mentioning that the the -i, --inplace option is only available with GNU sed. – Anthony Geoghegan Jul 06 '17 at 10:48
  • 1
    @AnthonyGeoghegan Oh, I didn't know that since I never had to use any sed other than GNU. – Maya Jul 06 '17 at 10:49
  • 2
    @AnthonyGeoghegan that is not true... other implementations support -i too, but syntax varies... see https://unix.stackexchange.com/questions/92895/how-to-achieve-portability-with-sed-i-in-place-editing and https://stackoverflow.com/questions/5694228/sed-in-place-flag-that-works-both-on-mac-bsd-and-linux – Sundeep Jul 06 '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 06 '17 at 12:19