Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash AWK或SED替换特定列中字母之间的空格_Bash_Shell_Awk_Sed - Fatal编程技术网

Bash AWK或SED替换特定列中字母之间的空格

Bash AWK或SED替换特定列中字母之间的空格,bash,shell,awk,sed,Bash,Shell,Awk,Sed,我有一个内嵌如下: 填充: INM00042170 28.2500 74.9167 290.0 CHURU 2015 2019 2273 INM00042182 28.5833 77.2000 211.0 NEW DELHI/SAFDARJUNG 1930 2019 67874 INXUAE05462 28.6300 77.2000 216.0 NEW DELHI

我有一个内嵌如下:

填充:

INM00042170  28.2500   74.9167  290.0    CHURU                          2015 2019   2273 
INM00042182  28.5833   77.2000  211.0    NEW DELHI/SAFDARJUNG           1930 2019  67874
INXUAE05462  28.6300   77.2000  216.0    NEW DELHI                      1938 1942   2068
INXUAE05822  25.7700   87.5200   40.0    PURNEA                         1933 1933    179
INXUAE05832  31.0800   77.1800 2130.0    SHIMLA                         1926 1928    728
PKM00041640  31.5500   74.3333  214.0    LAHORE CITY                    1960 2019  22915
我想在第5列中用下划线替换两个单词之间的空格(例如:新德里变为新德里)。我希望输出如下

输出文件:

INM00042170  28.2500   74.9167  290.0    CHURU                          2015 2019   2273 
INM00042182  28.5833   77.2000  211.0    NEW_DELHI/SAFDARJUNG           1930 2019  67874
INXUAE05462  28.6300   77.2000  216.0    NEW_DELHI                      1938 1942   2068
INXUAE05822  25.7700   87.5200   40.0    PURNEA                         1933 1933    179
INXUAE05832  31.0800   77.1800 2130.0    SHIMLA                         1926 1928    728
PKM00041640  31.5500   74.3333  214.0    LAHORE_CITY                    1960 2019  22915

谢谢你

到目前为止你都试了些什么?我试了一个基本的,但不起作用,因为它把空格后的单词作为下一列。因此,当试图打印第5列时,它只显示“新建”。我对此没有太多的想法。使用GNU
awk
,您可以尝试使用
字段宽度
请参见
#!/bin/bash

# connect field 5 and 6 and remove those with numbers. 
# this returns a list of new names (with underscore) for 
# all cities that need to be replaced 
declare -a NEW_NAMES=$(cat infile | awk '{print $5 "_" $6}' | grep -vE "_[0-9]")

# iterating all new names
for NEW_NAME in ${NEW_NAMES[@]}; do
  OLD_NAME=$(echo $NEW_NAME | tr '_' ' ')
  # replace in file
  sed -i "s/${OLD_NAME}/${NEW_NAME}/g" infile
done