trim spaces and replacing value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trim spaces and replacing value
# 1  
Old 06-29-2009
trim spaces and replacing value

Hi,
I have a file origFile.txt with values:
origFile.txt
.00~ 145416.02~ xyz~ ram kishor ~? ~ ~783.9
.35~ 765.76~ anh reid~ kelly woodburg ~nancy ~ ~?

Now each row in the file has value for 7 columns with "~" as delimiter.
The requirement was
i)I need to erase the blank spaces between "~" and each column value.
ii) If a column value has a blank space in betwwn (e.g. "ram kishore"), then that blank space shouldn't be removed
iii)If there is no value / only blank spaces in between two consecutive delimiters i.e. "~", then there should
be a single blank space between those two delimiters
iv) If the column value is "?", then the "?" should be removed and the blank spaces should be trimmed for that column value.
i.e. there should not be any value in between the delimiters for that column value.

The output file modFile.txt should look like
modFile.txt
.00~145416.02~xyz~ram kishor~~ ~783.9
.35~765.76~anh reid~kelly woodburg~nancy~ ~

The script I wrote was
sed -e 's/\s*\~\s*/\~/g' -e 's/^\s*//g;' origFile.txt > modFile.txt

The above script solved only the first requirement.
Could anyone please help me in writing the shell script to solve all four requirements listed above?
# 2  
Old 06-29-2009
Try this:

Code:
awk -F"~" '
{  for (i=1;i<=NF;i++) {
     if(length($i)>1) {
       sub(/^ */, "", $i);sub(/ *$/, "", $i)
     }
     sub("?","",$i)
   }
}1' OFS="~" origFile.txt > modFile.txt

# 3  
Old 06-29-2009
Thanks

Thanks a lot @Franklin52
It works fine...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Trim trailing spaces from file

I have a file like this. hari,corporationbank,2234356,syndicate ravi,indian bank,4567900000000,indianbank,accese raju,statebank of hyderabad,565866666666666,pause Here each record has different record length and there are blank spaces... (8 Replies)
Discussion started by: kshari8888
8 Replies

2. Shell Programming and Scripting

Trim spaces

All, i am comparing the output of one command to a numberic if ] but my problem is the output of follwoing is but but has some leading columns. I don't have any problme in LINUX and HP-UX. But only in AIX i am getting the leading spaces. I have developed my script on LINUX but when... (4 Replies)
Discussion started by: rcc50886
4 Replies

3. Shell Programming and Scripting

trim spaces in unix for variable

HI Guys I have written a script using awk to split a file based on some identifier and renaming the file based on two values from specific length. ts a fixed width file. When I am trying to fetch the values a = substr($0,11,10) b = substr($0,21,5); i am getting spaces in a and b values .... (6 Replies)
Discussion started by: manish8484
6 Replies

4. Shell Programming and Scripting

trim spaces in a file

Hi, I'm new to shell programming. Need some help in the following requirement: I have a file origFile.txt with values: origFile.txt .00~ 145416.02~ xyz~ ram kishor .35~ 765.76~ anh reid~ kishna kerry Now each row in the file has value for 4 columns with "~" as... (7 Replies)
Discussion started by: badrimohanty
7 Replies

5. UNIX for Dummies Questions & Answers

Replacing the Spaces

Hi, i have a tab dilimeted file.The records are :header is having column names.I am facing the following issue : I want to convert the spaces only for header file into "_" in the unix shell but the problem is that if i use sed command all the blank spaces are getting replaced by "_". For... (3 Replies)
Discussion started by: Amey Joshi
3 Replies

6. Shell Programming and Scripting

need help in replacing spaces in a file

hi all this is the part i am facing a problem eg data: filename : tr1 + T 40 this is a sample record in that file ... the value of T can be anything, but will be a single character. i need to cut from field two, and i am using this command cut -d " " -f2 tr1 >tr3 and the o/p is ... (7 Replies)
Discussion started by: sais
7 Replies

7. UNIX for Advanced & Expert Users

TRIM spaces in shell

am get a value like ' 15' in a variable what is the easiest method i can follow to strip 15 out (3 Replies)
Discussion started by: anumkoshy
3 Replies

8. Shell Programming and Scripting

To Trim spaces at the end of line

Hi Friends, Can any one help with this issue: How to trim spaces for each line at the end, Like I have a file in this format. EMP1 SMITH 46373 5 STREET HOWARD 74636 EMP2 JONES 5454 { these are spaces ........} EMP3 SMITH 46373 5 STREET HOWARD 74636 EMP4 JON 2554 { these are... (1 Reply)
Discussion started by: sbasetty
1 Replies

9. Shell Programming and Scripting

Trim trailing spaces from each line in a file

Hello folks, Is there a simple way to trim trailing spaces from each line a file. Please let me know. Regards, Tipsy. (5 Replies)
Discussion started by: tipsy
5 Replies

10. Shell Programming and Scripting

Trim white spaces using awk

Hi, I have a CSV file with footer information as below. The third value is the number of records in the file. Sometimes it contains both leading and trailing white spaces which i want to trim using awk. C,FOOTER , 00000642 C,FOOTER , 00000707 C, FOOTER,... (2 Replies)
Discussion started by: mona
2 Replies
Login or Register to Ask a Question