dpy15285 2011-03-27 03:06
浏览 38
已采纳

帮助awk截断和填充

I have a long list of Unicode values that are semi-colon delimited. Here's an example:

E0027;TAG APOSTROPHE;Cf;0;BN;;;;;N;;;;;

All I need is the "E0027;" part.

So I first need to drop everything in the line AFTER the first semicolon, but in some cases the semicolon is after 4 digits, in other cases, (as above) it's after 5. If it were the same throughout I'd just truncate after a fixed number of chars. I've found lots of examples for doing various manipulations with awk but no regular expressions that seem to fit this particular case. Does anyone know what the proper syntax is? The logic is merely to keep everything BEFORE the first semicolon and to drop everything after it.

Then, for the resulting file, I need to add a leading 0 to the line if the number is only 4 chars. So for example:

8A9B;

Should become:

08A9B;

But the 5 digit values (such as the first example) should remain as is...no leading zero.

(Though would an extra leading zero make a difference if I'm using these values in HTML? Would it matter if I had:

&#x0E0027

Instead of:

&#xE0027

If these will be parsed identically by PHP and won't make a difference, I guess the latter part isn't so important (though with thousands of extra zeros it will bloat the size of the code.)

Thank you for any help in advance!

  • 写回答

5条回答 默认 最新

  • dpv46227 2011-03-27 03:35
    关注
    awk -F';' '$0=length($1)<5?"0" $1 FS:$1 FS'
    

    Proof of Concept

    $ echo "8A9B;TAG APOSTROPHE;Cf;0;BN;;;;;N;;;;;" | awk -F';' '$0=length($1)<5?"0" $1 FS:$1 FS'
    08A9B;
    
    $ echo "E0027;TAG APOSTROPHE;Cf;0;BN;;;;;N;;;;;" | awk -F';' '$0=length($1)<5?"0" $1 FS:$1 FS'
    E0027;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导