![sed: 값을 제거하고 끝에 삽입](https://linux55.com/image/207097/sed%3A%20%EA%B0%92%EC%9D%84%20%EC%A0%9C%EA%B1%B0%ED%95%98%EA%B3%A0%20%EB%81%9D%EC%97%90%20%EC%82%BD%EC%9E%85.png)
다음 문자열이 있습니다
"1 Michael"
"2 John" ...
나는 그것들을 다음과 같이 바꾸고 싶다.
"Michael 1"
"John 2" ....
어떻게 해야 하나요?
문자열은 쉘 변수에 저장되며 각 문자열은 별도의 줄을 나타냅니다.
따라서 echo "$var"가 호출되면 인쇄됩니다.
1 Michael
2 John
답변1
echo "$string" | sed -E 's/([[:digit:]]+) (.*)/\2 \1/'
답변2
공백으로 구분된 두 필드를 다음으로 바꿉니다 awk
.
awk '{ print $2, $1 }' file >outfile
출력은 공백으로 구분된 파일로 이름이 지정된 파일에 기록됩니다 outfile
.
시험:
$ cat file
1 Michael
2 John
$ awk '{ print $2, $1 }' file >outfile
$ cat outfile
Michael 1
John 2