开发者

unable to store multiple sed operations using pipe in a variable

开发者 https://www.devze.com 2023-03-09 19:08 出处:网络
I am not able to execute a multiple sed statement using pipe filters in a variable. i am trying to extract a path from a file and then working on that path to change a few letters within the path by

I am not able to execute a multiple sed statement using pipe filters in a variable. i am trying to extract a path from a file and then working on that path to change a few letters within the path by going through another sed statement.

the code looks like this:

for i in 1 2 3 4 5 6 7
do
  for j in F E I D
  do
  cpath="`sed -n "11,11p" <path.cfg | sed -n 's/voicex/voice'$i'/g; s/voicecolx/voicecol'$i'/g; s/" F "/" '$j' "/g;P'`"
  echo $cpath
   more $cpath 2>error.log | grep " '&j' " | wc -l | echo "The count of '$j' in file is: " `wc -l` 1>countdir/count$j.txt
  done
done

i saved this file in stest.sh. When i run the file, nothing happens. I ahve to press Ctrl+C to exit back to the command prompt.

Let me also provide the path.cfg file:

MPAY
/var/xacct_data/xxxx/log_flattener/xxxx/logfile_current

FAFF
/var/xacct_data/faff/faff1/log_flattener/faffsnp1/ logfile_current
/var/xacct_data/faff/faff1/log_flattener/faffdbt1 /logfile_current
/var/xacct_data/faff/faff1/log_flattener/fafftxn1 /logfile_current
/var/xacct_data/faff/faff2/log_flattener/faffdbt2/ logfile_current

VOICE
/var/xacct_data/voice/voicex/log_flattener/ voicecolx/logfile_current
/var/xacct_data/voice/voicex/log_flattener/ voiceprox/logfile_current
/var/xacct_data/voice/voicex/log_flattener/ voicedisx/logfile_current

GPRS
/var/xacct_data/gprs/gprsx/log_flattener/gprscolx/logfile_current
/var/xacct_data/gprs/gprsx/log_flattener/gprsprox/logfile_current
/var/xacct_data/gprs/gprsx/log_flattener/gprsdisx/logfile_current

IPC
/var/xacct_data/ipc/ipcx/log_flattener/ipccolx/logfile_current
/var/xacct_data/ipc/ipcx/log_flattener/ipcprox/logfile_current
/var/xacct_data/ipc/ipcx/log_flattener/ipcdisx/logfile_current

SMS
/var/xacct_data/smsc/smscx/log_flattener/smsccolx/logfile_current
/var/xacct_data/smsc/smscx/log_flattener/smscprox/logfile_current
/var/xacct_da开发者_JAVA技巧ta/smsc/smscx/log_flattener/smscdisx/logfile_current


In order to get specific advice, you need to provide a sample path.cfg, explain what's wrong with the script current's behavior and what the expected behavior is. That being said, there are a few red flags in your code.

  • Use $(…) instead of `…` for command substitutions. The parsing of backquotes is rather fiddly, and you may run into issues with nested quotes.
  • sed -n "11,11p" means print the 11th line only. Is this what you really wanted? I guess not, but knowing neither the format of path.cfg nor what you intended I can't tell you what that command should be.
  • You should always use double quotes around variable substitutions, e.g. echo "$cpath". Only leave them out if you know it would be wrong to have them, do put them if you have no good reason to omit them. I don't think this is causing any problem here, but it's a good habit to get into when writing shell scripts.
  • Why are you calling more? Piping more into another program is useless. Just write <"$cpath" grep " '&j' " | …, assuming you did want to display all the lines containing the string '&F' (and so on for other values of $j) in the file called $cpath.
  • Still on this line, grep PATTERN | wc -l can just be written grep -c PATTERN.
  • Still on this line, you're running wc -l on the output of wc -l, which isn't useful. I supposed you meant to run it only once. Here are two ways to write this line; the first is closed to your structure, the second is rather clearer:

    <"$cpath" grep -c " '&j' " | echo "The count of '$j' in file is:  $(cat)" >"countdir/count$j.txt"
    echo "The count of '$j' in file is:  $(<"$cpath" grep -c " '&j' ")" >"countdir/count$j.txt"
    
0

精彩评论

暂无评论...
验证码 换一张
取 消