2

Input file having and two records and three fields, second attribute contains new line characters.I want enclose every field value with double quotes.

Input File:

100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24

Required output should be like below

"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"
  • Are you able to modify the code or output format of whatever produces this data? – Kusalananda Jan 24 at 10:04
  • Wait, so there are linebreaks in the delimited strings?! – Panki Jan 24 at 10:16
  • Yes, OP says: second attribute contains new line characters. – pLumo Jan 24 at 10:19
  • @RoVo what is "second attribute" anyway? – Niko Gambt Jan 24 at 11:45
  • 1
    You should ask OP ... but I guess it means second field of his |sv. – pLumo Jan 24 at 11:58
1

You can try with this awk :

awk -F'\n' '!f{$1="\""$1;f=1}{f=f+gsub("[|]","\"|\"")}f==3{$0=$0"\"";f=0}1' infile
  • Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'\n' '!f{$1="\""$1;f=1}{f=f+gsub("[|]","\"|\"")}f==$Fieldcnt{$0=$0"\"";f=0}1' – chava kumar Jan 26 at 11:46
  • @chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v. – ctac_ Jan 26 at 12:58
  • I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'\n' '!f{$1="\""$1;f=1}{f=f+gsub("[|]","\"|\"")}f==NOOFATTRIBUTES{$0=$0"\"";f=0}1' – chava kumar Jan 26 at 14:08
1

You can do it with perl:

perl -0pe 's/([^|]*)\|([^|]*)\|([^|\n]*)(\n|$)/"\1"|"\2"|"\3"\4/g' input_file
  • -0 read file at once, not line by line.
  • -p print the line in the end
  • -e the expression
  • s/pattern/replacement/g Replace pattern with replacement
0
$ cat input.txt | tr "\n" "\t" \
  |awk -v FS="|" -v OFS="|" '{for(i=1;i<=NF;i++) $i="\""$i"\"";}1' \
  |tr "\t" "\n"
  • tr transform new line to a tab. So the input is linearized
  • We tell awk to handle the | as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed
  • tr converts the tab back to a new line
  • Thank you for help, but this code didnt work in my case. – chava kumar Jan 26 at 11:59
  • "didn't work" is never a good error description. Could you please describe what doesn't work? – finswimmer Jan 26 at 12:07
  • Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "\n" "\t" |awk -v FS="|" -v OFS="|" '{for(i=1;i<=NF;i++) $i=""$i"";}1' |tr "\t" "\n" – chava kumar Jan 26 at 12:33
  • Could you try my edited version? – finswimmer Jan 28 at 4:58

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.