0

I have a pipe delimited file with a sample lines like below;

/u/chaintrk/bri/sh/picklist_autoprint.sh|-rwxrwxr-x|bdr|bdr|2665|Oct|23|14:04|3919089454
/u/chaintrk/bri/sh/generate_ct2020.pl|-rwxrwxr-x|bdr|bdr|15916|Oct|23|14:04|957147508

is there a way that awk or sed can transform the lines into the output like below where the pipe between the month and the date was replaced by space?

/u/chaintrk/bri/sh/picklist_autoprint.sh|-rwxrwxr-x|bdr|bdr|2665|Oct 23|14:04|3919089454
/u/chaintrk/bri/sh/generate_ct2020.pl|-rwxrwxr-x|bdr|bdr|15916|Oct 23|14:04|957147508
Dren
  • 319
  • 2
  • 14
  • Your input and output appear to be identical to me. Do you care to update your question? – Tim Biegeleisen Oct 28 '15 at 05:38
  • on the input there is a pipe between the month and date (Oct|23) while on the output it needs to be replaced by a space (Oct 23) – Dren Oct 28 '15 at 05:42

2 Answers2

0

With GNU sed:

sed -E 's/(\|[A-Z][a-z]{2})\|([0-9]{1,2}\|)/\1 \2/' file

Output:

/u/chaintrk/bri/sh/picklist_autoprint.sh|-rwxrwxr-x|bdr|bdr|2665|Oct 23|14:04|3919089454
/u/chaintrk/bri/sh/generate_ct2020.pl|-rwxrwxr-x|bdr|bdr|15916|Oct 23|14:04|957147508

If you want to edit file "in place" add sed's option -i.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • i got an invalid option when i tried it sed -E 's/(\|[A-Z][a-z]{2})\|([0-9]{1,2}\|)/\1 \2/' testlist.txt sed: invalid option -- E – Dren Oct 28 '15 at 05:54
  • Try this with a non GNU sed: `sed 's/\(|[A-Z][a-z][a-z]\)|\([0-9]\+|\)/\1 \2/' file` – Cyrus Oct 28 '15 at 06:01
  • Thanks it worked. Can you kindly explain how the command was able to distinguish which pipe will it replace from the line. – Dren Oct 28 '15 at 06:13
  • I have used a regex to describe what is to be searched for and replaced. Search for a pipe, followed by one capital letter, followed by two small letters, followed by a pipe, followed by at least one number and followed by a pipe. See also: [The Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean/22944075#22944075) – Cyrus Oct 28 '15 at 06:21
  • @Cyrus please add the regex explanation to your answer it will be useful for future readers. Thanks. – Arnab Nandy Oct 28 '15 at 07:36
0

Yes, it is possible to change a "|" with an space.
The real problem is to identify which of the field(s) to change.

Are those always the 6th and 7th? If so, this works:

awk -vFS='|' '{sub($6"|"$7,$6" "$7)}1' file

Are those with a text Upper-lower-lower followed by a 1 or 2 digits? If so, this other works:

gawk '{c="[|]([[:upper:]][[:lower:]]{2})[|]([0-9]{1,2})[|]";print gensub(c,"|\\1 \\2|",1,$0)}' file