在AIX中删除管道分隔文件中特定列中的空格 [英] Removing blank spaces in specific column in pipe delimited file in AIX

查看:252
本文介绍了在AIX中删除管道分隔文件中特定列中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好。长期的读者,第一次发电子邮件的人,请保持谦逊。

Good morning. Long time reader, first time emailer so please be gentle.

我正在使用AIX 5.3,并且有一个42列管道分隔文件。第15和第25栏中有电话号码。

I'm working on AIX 5.3 and have a 42 column pipe delimited file. There are telephone numbers in columns 15 & 16 (land|mobile) which may or may not contain spaces depending on who has keyed in the data.

我需要从第15列和第16列中删除这些空间。仅16,即

I need to remove these space from columns 15 & 16 only ie

Column 15   |   Column 16 **Currently**
01942 665432|07865346122
01942756423 |07855 333567
Column 15   |   Column 16 **Needs to be**
01942665432|07865346122
01942756423|07855333567

我很快不幸的是,肮脏的脚本实在是快不过了,因为它是一个while循环,读取每行,剪切管道定界符上的字段,将其分配给变量,使用第15列和第sed列中的sed。 16仅去除空格,然后将其写到新文件中,即

I have a quick & dirty script which unfortunately is proving to be anything but quick because it's a while loop reading every single line, cutting the field on the pipe delimiter, assigning it to a variable, using sed on column 15 & 16 only to strip blank spaces then writing it out to a new file ie

cat $file | while read 

output

do

.....

fourteen=$( echo $output | cut -d'|' -f14 )

fifteen=$( echo $output | cut -d'|' -f15 | sed 's/ //g' )

echo ".....$fourteen|$fifteen..." > $new_file

done

我知道必须有更好的方法这可能使用AWK,但任何人都可以提出任何建议,因为脚本原本需要半小时才能处理176,000条记录。

I know there must be a better way to do this, probably using AWK, but am open to any kind of suggestion anyone can offer as the script as it stands is taking half an hour plus to process 176,000 records.

谢谢

推荐答案

是的, awk 更适合这里

$ cat ip.txt 
a|foo bar|01942 665432|07865346122|123
b|i j k |01942756423 |07855 333567|90870

$ awk 'BEGIN{FS=OFS="|"} {gsub(" ","",$3); gsub(" ","",$4)} 1' ip.txt 
a|foo bar|01942665432|07865346122|123
b|i j k |01942756423|07855333567|90870




  • BEGIN {FS = OFS = |} | 设置为输入和输出字段分隔符

  • gsub(,,$ 3)仅将第3列的所有空格替换为空

  • gsub(,,$ 4)替换全部仅第4列没有空格的空格

  • 1 惯用的方式来打印输入记录(包括所做的任何修改)

    • BEGIN{FS=OFS="|"} set | as input and output field separator
    • gsub(" ","",$3) replace all spaces with nothing only for column 3
    • gsub(" ","",$4) replace all spaces with nothing only for column 4
    • 1 idiomatic way to print the input record (including any modification done )
    • 3 4 更改为任何值您需要的字段

      Change 3 and 4 to whatever field you need



      如果不影响第一行,请添加条件


      In case first line should not be affected, add a condition

      awk 'BEGIN{FS=OFS="|"} NR>1{gsub(" ","",$3); gsub(" ","",$4)} 1' ip.txt 
      

      这篇关于在AIX中删除管道分隔文件中特定列中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆