1

I have a comma separated file that looks similar to his format:

aa.com,1.21.3.4,string1 string2 K=12     K2=23  K3=45 K4=56
bb.com,5.6.7.8,string1 string2 K=66     K2=77  K3=88 K4=99

I want to take the third column which contains strings separated by spaces. I want to process the file to separate the third columns first two strings by a comma and ignore the rest of the strings in column 3. The first two fields do not contain spaces. Please note the number of strings in the 3rd column is not fixed to all records. In this example, it is 6 strings separated by 5 spaces. But it can be more or less.

All that I need is to take the 3rd columns first two strings, separate them by a comma, and ignore the rest of column 3 strings.

aa.com,1.21.3.4,string1,string2
bb.com,5.6.7.8,string1,string2
  • put in other words, you could use space as field separator and print first two fields with comma as separator - assuming the comma separated fields don't have spaces in them... please add what you've tried yourself to solve this.. – Sundeep Aug 25 '18 at 9:41
  • @Sundeep this is a file generated by other programs. So I have to deal with the output as is. The file is too large that I can not do this manually. I'm new a Linux user but have t use it for a tasks. – user9371654 Aug 25 '18 at 9:42
  • oh ok, in that case we cannot assume space to be absent from first two fields of comma separated values and your description indicates the best approach to solve.. awk/sed can be used here.. but you need to show what code you've tried yourself.. otherwise it comes across as free coding service request.. – Sundeep Aug 25 '18 at 9:49
  • @Sundeep the first two fields do not contain spaces. – user9371654 Aug 25 '18 at 9:55
  • in that case solution would indeed be simple, but that answer was deleted.. please edit the question and highlight this point.. and in future, add what you have tried while asking question.. see unix.stackexchange.com/tags/awk/info for learning resources – Sundeep Aug 25 '18 at 9:59
1

try:

awk '{print $1, $2}' OFS=, infile
aa.com,1.21.3.4,string1,string2
bb.com,5.6.7.8,string1,string2

If in such a case you had white-spaces in first or second fields, you would do:

awk -F, '{ match($3, /[^ ]* +[^ ]*/); 
           bkup=substr($3, RSTART, RLENGTH);
           gsub(/ +/, ",", bkup); # replace spaces with comma
           print $1, $2, bkup
}' OFS=, infile

Explanation: read in man awk:

match(s, r [, a])  
          Return the position in s where the regular expression r occurs, 
          or 0 if r is not present, and set the values of RSTART and RLENGTH. (...)

substr(s, i [, n])
          Return the at most n-character substring of s starting at I.
          If n is omitted, use the rest of s.

RSTART
          The index of the first character matched by match(); 0 if no
          match.  (This implies that character indices start at one.)

RLENGTH
          The length of the string matched by match(); -1 if no match.
  • Thanks. The first command works as expected since I do not have spaces in the first two columns. For the sake of understanding, I understand that OFS=, means replace space separator (the default) with the comma. But I do not get how to command prints four columns while the print command contains only two variables? which columns do $1, $2 refer to? how the commands gets to understand to ignore the rest of the strings in column 3 in the original file? – user9371654 Aug 25 '18 at 17:15
  • @user9371654 that's still column1 and column2 printing based on awk default space (or Tab) seperator and OFS=, is telling set Output Field Seperator to comma, so thess two columns printing with comma separated – αғsнιη Aug 25 '18 at 23:12
0

Try this:

awk -F '[, ]' '{print $1","$2","$3","$4}' file
aa.com,1.21.3.4,string1,string2
bb.com,5.6.7.8,string1,string2
0

You could do this as follows :

sed -ne 's/[[:blank:]]\{1,\}/,/;s//\n/;P' input-file.txt 
0
awk -F "[, ]" '{print $1,$2,$3,$4;OFS=","}' file

F "[, ]" Will take both space and comma as field separator and ;OFS="," will set Output field separator as comma.

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.