English 中文(简体)
在模式和bash中下一次出现的相同模式之间添加带文本的行
原标题:Adding line with text between pattern and next occurence of the same pattern in bash
  • 时间:2011-02-06 21:15:37
  •  标签:
  • bash
  • sed
  • awk

我正在编写一个bash脚本,用于修改一个如下所示的文件:

--- usr1 ---
data data data data
data data data data
data data data data
--- usr2 ---
data data data data
data data data data
--- usr3 ---
data data data data
--- endline ---

One question is: How to add next user line --- usrn --- after last user data lines? Second one is: How to delete specific user data lines (data lines and --- userx ---) i.e. I would like to delete usr2 with all his data set.

It must work on bash 2.05 :) and I think it will use awk or sed, but I m not sure.
A little edit here: In fact usernames are not numbered. We don t know what users will come up with. We only know that the name will be inside --- pattern

最佳回答

给定变量中的用户名:

username="kasper"

删除用户分区:

sed "/$username/{:a;N;/
--- [^[:blank:]]* ---$/{s/.*
//;b};ba}" inputfile

或者对于sed的某些版本(编辑):

sed -e "/$username/{" -e  :a  -e  N  -e  /
--- [^[:blank:]]* ---$/{s/.*
//  -e  b  -e  }  -e  ba  -e  }  inputfile

编辑:一种可能的变体,以适应前导和尾随空格:

sed -e "/$username/{" -e  :a  -e  N  -e  /
[[:blank:]]*---[[:blank:]]*[^[:blank:]]*[[:blank:]]*---[[:blank:]]*$/{s/.*
//  -e  b  -e  }  -e  ba  -e  }  inputfile

添加下一个用户部分:

sed "s/--- end/--- $username ---
data data data data
data data data data
&/"

sed "/--- end/i--- $username ---
datadata data data
data data data data
"

If your version of sed supp或ts in-place changes, you can do:

sed -i ...

Otherwise, you ll have to use a temp或ary file:

sed ... inputfile > tmpfile && mv tmpfile inputfile

It s best to use a utility like mktemptempfile to create a temp或ary file and use the name provided.

问题回答

第二部分的awk解决方案:

awk -v del="usr2"  match($0,/^--- (.*) ---$/,a) {p = (a[1] != del)} p 

如果头与要删除的用户匹配,则会关闭p标志,否则会打印每一行。

Plain shell can handle this

更新了答案,包括第二个要求。。。

#!/bin/sh

NEWUSER= John Doe 
NEWUSERDATA= how now brown cow 
REMOVEUSER= usr2 

state=COPY
while read x; do
  case "$state/$x" in
    *"/--- endline ---")
      echo --- $NEWUSER ---
      echo "$NEWUSERDATA"
      state=COPY
      ;;
    COPY/*)
      if [ "$x" == "--- $REMOVEUSER ---" ]; then
        state=REMOVE
      fi
      ;;
    REMOVE/---*)
      state=COPY
      ;;
  esac
  if [ $state != REMOVE ]; then
    echo "$x"
  fi
done

用法类似于:shnewuser.sh<;usertable.txt>;newusertable.txt

顺便说一下,还有一种编写shell脚本的替代风格,我可以称之为“gnu-configure”格式。在“替代风格”中,主循环为:

while read x; do
  case "$state/$x" in
    *"/--- endline ---")
      echo --- $NEWUSER ---
      echo "$NEWUSERDATA"
      state=COPY;;
    COPY/*)
      [ "$x" == "--- $REMOVEUSER ---" ] && state=REMOVE;;
    REMOVE/---*)
      state=COPY;;
  esac
  [ $state != REMOVE ] && echo "$x"
done




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签