| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
On 2016-02-09 16:25, MOKRANI Rachid [email protected]
> Please, some assistance for the sed command to get the result below.
>
> Input.txt
> "Paul";"01 26 30 69 69";"NY"
> "Dan";"05 26 30 69";"CA"
> "Jane";"26 30 69";"SL"
> "Bill";"03 26 30 69 69";"BT"
> "Steve";"03-26-30-69-69";"BT"
> "Daniel";"NA";"BO"
> "Karen";"01/02/03";"YO"
>
> I would like to have (only line with 10 digits) - remove all
> field less or greater than 10 digits and all fields with not
> numeric
>
> Output.txt
> "Paul";"01 26 30 69 69";"NY"
> "Dan";"";"CA"
> "Jane";"";"SL"
> "Bill";"03 26 30 69 69";"BT"
> "Steve";"";"BT"
> "Daniel";"";"BO"
> "Karen";"";"YO"
While ugly, you can do it in sed:
sed '/^\("[^"]*";"\)\(\( *[0-9]\)\{10\}\)\(".*"\)/!s/^\("[^"]*";"\)[^"]*\(".*"\)/\1\2/'
Might be clearer in awk:
awk -vOFS=\; -F\; '$2 !~ /^"(\s*[0-9]){10}\s*"$/{$2="\"\""}{print}'
which roughly translates to
$2 where field #1
!~ doesn't contain
/^"(\s*[0-9]){10}\s*"$/ 10 digits (ignoring optional spaces)
{$2="\"\""} set the 2nd column to double-quotes
{print} and print the resulting row
The "-vOFS=\;" and "-F\;" specify the output column-delimiter and the
input column-delimiter.
-tim