Hai masters,
If a file contains content of 2000 lines,
from which i need to remove the first n characters or first n spaces from each line of the file.
If suppose to remove n characters or first n spaces from a single line means, just use the command nx.
But from the above scenario, how we need to get the solution..
Thanks & Regards,
Arun
Hi
To replace the first n characters or spaces in the beginning of each line, you can use the command sed
Example:
sed 's/^ /zzz/g' ./test.log > test1.log
By this command, you will be able to substitute the first three spaces of each line by the string zzz.
Regards
@Hammadi dali,
No man, its not working..
See
Yes by using your script,
E.g
echo " sdfsdf" | sed 's/^ /zzz/g'
Then the o/p is zzzsdfsdf.
But for example if a file contains content as follows,
181 last
182 first
183 begin
184 apropos lst
185 lstat
186 stat
187 apropos last
188 lastcomm
189 dir
190 lastcomm
191 lastlog
192 errno
193 last
Now i want to remove first 7 characters from all the lines whether it is space or number.
I tried this one
sed -e "s/.//1" -e "s/.//1" -e "s/.//1" -e "s/.//1" -e "s/.//1" -e "s/.//1" -e "s/.//1" linuxcommands.sql >xxx.sql
The above command replaces first 7 characters from all the lines of the file..
But still i am thinking to achieve this in some other way.
If u came to know some other process to arrive at this solution means,
Reply me.
An example to remove 2 leading spaces with sed:
sed 's/^ \{2\}//' file
Regards
Hai Franklin,
Fine...
Its working for replacing the spaces..
Is it possible to remove the leading n characters frm all the lines
Note: n characters may be spaces or number or characters.
Also i posted an example in the above posts...
This prints the lines from the 3th position (deletes the 1st 3 characters of a line):
sed 's/...//' file
With awk you can use the substr() function:
awk '{print substr($0, 3}' file
Regards
Hai Franklin.
Thank you Frank..
This one "linuxcommands.sql >new.sql " is working fine..
But that awk script is not woking.
Sorry, I forgot a parenthesis:
awk '{print substr($0, 3)}' file
Regards
Thats Fine man..
Ya, now its working fine..
Great Frank.