
아래 문자열이 있습니다.
"-소유자 -날짜 2017-10-10 -우선순위 20 -값 xyz -outputLocation "
무시하고 싶다-우선순위 20그리고-outputLocationbash 스크립트에서 이 매개변수를 구문 분석하는 동안 옵션을 사용합니다. sed/awk/grep을 사용하여 이를 수행하는 어떤 트릭이 있습니까?
참고 1: 매개변수에는 특정 순서가 없습니다. note2: -priority는 0~100 사이의 숫자일 수 있습니다. note3: -outputLocation . dir_name은 실행될 때마다 항상 변경됩니다.
답변1
사용argparse
매개변수를 쉽게 구문 분석할 수 있습니다.
parser = argparse.ArgumentParser()
parser.add_argument('-owner', action='store_true')
parser.add_argument('-date')
args = parser.parse_args()
print(args.accumulate(args.integers))
거기에서 일부 매개 변수를 무시하는 것은 문자 그대로 사소한 일입니다.
답변2
방법 1
여기서는 "-priority 20 -outputLocation" 내용을 공백으로 바꿉니다.
#!/bin/bash
for i in -priority 20 -outputLocation
do
sed -i "s/"$i"//g" /tmp/l.txt
done
방법 2
여기서는 값을 무효화하고 있습니다.
awk '{$5="";$6="";$NF="";print $0}' l.txt