2012-12-26 3 views
1

sed을 사용하여 파일의 문자열을 역순으로 원합니다. 그러나 표현식이 숫자와 특수 문자를 뒤집지 않도록하고 싶습니다.문자열을 역순으로 바꾸는 방법 줄에 숫자와 특수 문자를 쓰지 마십시오.

112358 is a fibonacci sequence... 
a test line 
124816 1392781 
final line... 

내 예상 출력은 다음과 같습니다 :

예를 들어, 다음과 같은 입력을 고려 나는 여러 가지 방법을 시도

112358 si a iccanobif ecneuqes... 
a tset enil 
124816 1392781 
lanif enil... 

,하지만 난 그것에 대해 정확한 표현을 찾을 수 없습니다. 나는 다음과 같은 표현을 시도했지만 그것은 전체 문자열을 반대했습니다

sed '/\n/!G;s/\([.]\)\(.*\n\)/&\2\1/;//D;s/.//' 
+1

는 무엇 일 것인가? –

+1

다른 사람이 같은 질문을했기 때문에 숙제입니까? (http://askubuntu.com/questions/232846/how-to-reverse-the-lower-case-characters-in-word-using-only -sed). – jfg956

답변

3

이 나오지 스크립트는 일을 할 것입니다 : 나는 이것에 대한 Perl을 사용

#!/usr/bin/sed 

# Put a \n in front of the line and goto begin. 
s/^/\n/ 
bbegin 

# Marker for the loop. 
:begin 

# If after \n is a lower case sequence, copy its last char before \n and loop. 
s/\n\([a-z]*\)\([a-z]\)/\2\n\1/ 
tbegin 

# If after \n is not a lower case sequence, copy it before \n and loop. 
s/\n\([^a-z]*[^a-z]\)/\1\n/ 
tbegin 

# Here, no more chars after \n, simply remove it before printing the new line. 
s/\n// 
3

. 코드는 훨씬 더 읽을 수 :

perl -pe 's/\b([A-Za-z]+)\b/reverse($1)/ge' file 

결과 : 입력 숫자가 포함 된 문자열, 예를 들어`this5string`가 포함되어있는 경우

112358 si a iccanobif ecneuqes... 
a tset enil 
124816 1392781 
lanif enil... 
관련 문제