Remove whitespaces in the n first characters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove whitespaces in the n first characters?
# 1  
Old 02-02-2011
Remove whitespaces in the n first characters?

I assume removing whitespaces in the n first characters of a string would be an easy task for sed? If so, how?
# 2  
Old 02-02-2011
Is this homework/classroom stuff? What have you tried so far?...

I got no idea for sed; nevertheless:
Code:
> cat infile
aa bb cc dd ee
$> awk '{a=substr($0,0,n); b=substr($0,n+1); gsub(/ /,"",a); print a b}' n=7 infile
aabbcc dd ee

... where n=7 is your nth field.
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 02-02-2011
Hi,

Using 'sed' it is easy, yeah:
^ -> Begin of line (with one space after it)
\{n\} -> Number of spaces to supress.

Code:
$ sed -e 's/^ \{4\}//' infile

Regards,
Birei
# 4  
Old 02-02-2011
Quote:
Originally Posted by zaxxon
Is this homework/classroom stuff? What have you tried so far?...

I got no idea for sed; nevertheless:
Code:
> cat infile
aa bb cc dd ee
$> awk '{a=substr($0,0,n); b=substr($0,n+1); gsub(/ /,"",a); print a b}' n=7 infile
aabbcc dd ee

... where n=7 is your nth field.
Thanks, I will try that. Smilie
# 5  
Old 02-02-2011
Hi.

As seen in other threads, posting representative sample data and expected output is usually the shortest path to the best solutions ... cheers, drl
# 6  
Old 02-02-2011
I second what drl says and you still did not tell if this is homework/classroom stuff.
# 7  
Old 02-02-2011
Sorry.

After see the solution of zaxxon, mine was wrong. I missunderstood your question.

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove whitespaces between delimiter and characters

Hello All, I have a pipe delimited file and below is a sample data how it looks: CS123 | | || 5897 | QXCYN87876 As stated above, the delimited files contains sometimes only spaces as data fields and sometimes there are extra spaces before/after numeric/character data fields. My requirement... (4 Replies)
Discussion started by: amvip
4 Replies

2. Shell Programming and Scripting

Remove first 2 characters and last two characters of each line

here's what im trying to do. i have a file containing lines similar to this: data.txt: 1hsRmRsbHRiSFZNTTA1dlEyMWFkbU5wUW5CSlIyeDFTVU5SYjJOSFRuWmpia0ZuWXpKV2FHTnRU 1lKUnpWMldrZFZaMG95V25oYQpSelEyWTBka2QyRklhSHBrUjA1b1kwUkJkd3BOVXpWM1lVaG5k... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

remove first few characters from each line

Hi, I have a file with lines like below. I need to remove first few characters from each line until a date format is found. 05/06/12 20:47:02 GUMGUY@98.192.174.74{42B42A72AC955F5926621273E3A15059.tomcat2}TP-Processor15 LogExchUsage: ERROR: 05/06/12 20:47:02... (8 Replies)
Discussion started by: ratheeshjulk
8 Replies

4. UNIX for Dummies Questions & Answers

How do I remove ^M characters with VI

I have a file with all kinds of ^M at the end of each line. How the heck can these be removed? I tried a global search and replace, but it doesn't seem to work. Thanks! (8 Replies)
Discussion started by: HmmBerger
8 Replies

5. Shell Programming and Scripting

Remove characters from string

Hi I am new in shell scripting and i want to manipulate a string. I have a string tha looks like: /home/nteath/file.txt I want to remove everything until the last "/" , to keep only the filename. e.g. /home/nteath/file.txt output: file.txt Thanks (2 Replies)
Discussion started by: nteath
2 Replies

6. Shell Programming and Scripting

remove last 4 characters from a string

I'm tring to remove the last 4 characters from strings in a file i.e. cat /tmp/test iwishicouldremovethis icouldremovethos so i would end up with the last 4 characters from each of the above i.e. this thos I thought of using cut -c ... but I'm not sure how many characters will... (7 Replies)
Discussion started by: josslate
7 Replies

7. UNIX for Advanced & Expert Users

remove characters

hi i have a file with these strings: 123_abc_X1116990 how to get rid of 123_abc_ and keep only X1116990? I have columns of these: 123_abc_X1134640 123_dfg_X1100237 123_tyu_X1103112 123_tyui_X1116990 thx (5 Replies)
Discussion started by: melanie_pfefer
5 Replies

8. UNIX for Dummies Questions & Answers

Remove whitespaces between comma separated fields from file

Hello all, I am a unix dummy. I am trying to remove spaces between fields. I have the file in the following format 12332432, 2345 , asdfsdf ,100216 , 9999999 12332431, 2341 , asdfsd2 ,100213 , 9999999 &... (2 Replies)
Discussion started by: nitinbjoshi
2 Replies

9. UNIX for Dummies Questions & Answers

How to remove Characters before '~'

Hi, I am having a file which contains records as follows: DETAIL_KEY~12344|ACTIVE_PASSIVE~Y|AVG_SIZE_OF_RESPONSE~123123131 DETAIL_KEY~12344|ACTIVE_PASSIVE~Y|AVG_SIZE_OF_RESPONSE~123123131 DETAIL_KEY~12344|ACTIVE_PASSIVE~Y|AVG_SIZE_OF_RESPONSE~123123131... (4 Replies)
Discussion started by: Amey Joshi
4 Replies

10. Shell Programming and Scripting

help me to remove the whitespaces between the lines ...urgent

Hello all, i have a problem. please help me to remove the white spaces and tabs betweeen line. i.e., file1 contains some text.. text starts_hdsffdsd sdfsddssdds******** sdfsdsd*********** sdfsdsdfsdsdfsdsds*** ****fsd_test_ends one or 2 blank lines (* indicates white spaces or tabs) ... (5 Replies)
Discussion started by: kumar1
5 Replies
Login or Register to Ask a Question