Environment
RedHat Linux 9 + vwware 8.0 + SSH 3.2.9
Problem
Suppose there is a CSV (comma separated value) file with some of the information we need, but the format is problematic, and the current data is in the following order:
Name, company name, state abbreviation, ZIP code, now we want to talk about the data and rearrange it for use in one of our software,
The format required is: name, state abbreviation-zip code, company name.
That is, we're going to resize the column order and combine two columns to form a new column. In addition, our software cannot accept commas before any spaces (including spaces and tabs) are
We also have to remove all the spaces before and after the comma.
The original file is like this:
Bill Jones,??? Hi-tek Corporation, huh? CA, 95011
Sharon Lee Smith, huh? Design works Incorporated,? CA, 95012
B. Amos??,? Hill Street Cafe, huh? CA, 95013
Alexander Weatherworth, huh? The crafts Store, huh? CA, 95014
We want to make it look like this:
Bill Jones,ca 95011,hi-tek Corporation
Sharon Lee SMITH,CA 95012,design Works Incorporated
B. Amos,ca 95013,hill Street Cafe
Alexander weatherworth,ca 95014,the Crafts Store
Solve
Step One: Save the source file as a text document, such as Test.txt
Step two: Upload the test.txt to Linux, where you can use the SSH file upload function.
Step three: Will test.txt with VI open www.bianceng.cn
Step four: Execute the following command (note Enter a colon when entering a command, exit save input Wq, exit without saving using Q or q!):
1 Remove all the question marks (?) )
%s/?//g
2 Remove all spaces before and after commas
%s/[\t]*,[\t]*/,/g
3 Implementation Features
%s/\ ([^.] *\),\([^.] *\),\([^.] *\),\([^.] *\)/\1,\3 \4,\2/g
Implementation effect