Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove first 2 characters in a flat file using a unix script.

Status
Not open for further replies.

hugheskbh

Programmer
Dec 18, 2002
37
US
Hi,

I was wondering if there's a way to read a flat file and strip out the first 2 characters in a unix script.

Thanks
 
Hi Feherke,

I'm really not a unix programmer, could you please explain how it works. I basically have to read a flat file and strip out the first 2 characters in each record. Then move either move the record over 2 spaces or write to another file.

Thanks
 
Strip out the first two characters or the first two characters in each record (I assume record = line) are two different things.

sed is a stream-editor.
-i means 'modify in place'
1 means: modify just first line
s means: substitute
/.. means substitute two characters
// means substitute with nothing

to change every line in file xy:
Code:
sed 's/^..//g' xy > xy.new
here I don't use -i, because I redirect the output to xy.new.
^.. means two characters at the beginning of line
/g means 'modify globally'.

seeking a job as java-programmer in Berlin:
 
Thanks to both of you , this works fine. Stefanwagner I used your example to output to a new file. My only other questions is how can I place this in a unix script because after the file is created, I have to do other things with it using a script.

Thanks
 
Just include it in your script as a line before your 'other things'. Or do you mean (for example) how do I use vi to create a shell script?
 
Hi

stefanwagner said:
/g means 'modify globally'.
Correct. But this means to keep matching and replacing as many times as possible, not to stop after the first match and replace. But while the expression starts with carrot ( ^ ), only one match is possible. And while is impossible to not match the first two characters of the line, the carrot is not needed.

hugheskbh said:
[gray](...)[/gray] read a flat file and strip out the first 2 characters [gray](...)[/gray]

[gray](...)[/gray] read a flat file and strip out the first 2 characters in each record.
The two sentences have different meaning.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top