Replacing the new character with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing the new character with spaces
# 1  
Old 10-06-2009
Data Replacing the new character with spaces

Hi Experts,

We are facing some while loading the "csv" file to target table.Some of the records are having values as :

HTML Code:
Account number,Name,Address
"123","XYZ","302 Street,Washington,US"
"456","PQR"," 3233 Some Street,
Washington,US"
In the above file instead reading only two records it is reading 3 records... Smilie
Is there anyway to remove the "new line character' and replace it with the "space".

Thanks in Advance!!
Amey
# 2  
Old 10-06-2009
Try this:

Code:
awk 'NR>1 && /,$/{printf("%s ",$0);next}1'  file > newfile

# 3  
Old 10-06-2009
Thanks for the reply!!!

Actually,we have to first find which records are having this issues and if the delimiter does not matches with specified delimiter then we have to check for the next line.
For example:
HTML Code:
Account Number,Name,Address,Comments
"123","XYZ","302 Street,Washington,US","NA"
"456","PQR"," 3233 Some Street,
Washington,US","is Defaulter"
"3434","sdsd","3233 Some Street,
Washington,US","Is not Defaulter"
"87856","sdsd","3233 Some Street,Washington,US","Is not Defaulter"
For this, we have develop the script as follows:
HTML Code:
cat file_name.txt| while read line
do
val_delimiter=`echo $line |awk -F"," '{ print NF }'`
if [ $val_delimiter -ne 3 ]
then
echo we have to take the next line and again check the value
fi
Is there any other simple way to find which reocrds are having 'new line character' Smilie and if these records are having the 'new line character concate it with the next line Smilie

Again thanks for all the help!!
amey
# 4  
Old 10-06-2009
Code:
$sed 'N;s/,\n/ /g' filename

# 5  
Old 10-06-2009
Data

Thanks for the reply,but the "sed" command is not working ..Smilie Smilie

Also when checking the number of delimiters the awk command is taking the count of the delimiter which are in doublle qoutes(")..so instead of 3 commas the ouput of the command "val_delimiter=`echo $line |awk -F"," '{ print NF }'`" is giving as 5. Smilie

We are not able to find the alternate apporoach to find the records which are having 'new line' character in between ...Smilie Smilie

Thanks!
Amey
# 6  
Old 10-06-2009
It works for me.

Check the execution and its output.

Code:
$ cat a
Account Number,Name,Address,Comments
"123","XYZ","302 Street,Washington,US","NA"
"456","PQR"," 3233 Some Street,
Washington,US","is Defaulter"
"3434","sdsd","3233 Some Street,
Washington,US","Is not Defaulter"
"87856","sdsd","3233 Some Street,Washington,US","Is not Defaulter"

$ sed 'N;s/,\n/ /g' a
Account Number,Name,Address,Comments
"123","XYZ","302 Street,Washington,US","NA"
"456","PQR"," 3233 Some Street Washington,US","is Defaulter"
"3434","sdsd","3233 Some Street Washington,US","Is not Defaulter"
"87856","sdsd","3233 Some Street,Washington,US","Is not Defaulter"

Is this what you expected?

If still error, what is the error it is showing?
# 7  
Old 10-06-2009
Hi,

Try this, It will check number of fields and base on that it will take next line

Code:
#!/bin/sh

flag="N"
while read line
do

        if [ "$flag" == "Y" ]
        then
                echo $line
                flag="N"
                continue
        fi

        res=`echo $line | awk -F',"' '{print($4)}'`
        if [ -z "$res" ]
        then
                echo -n $line
                flag="Y"
                continue
        else
                echo $line
                flag="N"
        fi
done < test_sample

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with replacing characters without moving the spaces.

Could you please advise what is the best way to edit a file without disrupting the spaces? For example: I have within my file the value below, wherein I wanted to change VALUE2 to VALUETEST. The total characters on the field of VALUE2 is 15 characters. VALUE1|VALUE2<9 spaces>|VALUE3 but... (3 Replies)
Discussion started by: asdfghjkl
3 Replies

2. Shell Programming and Scripting

Replacing certain positions in lines with spaces

Hello, I have a file with hundreds of lines. Now I need to replace positions 750-766 in each line (whatever there is there) with spaces... how can I do that? Which command to use? The result will be all the lines in the file will have spaces in positions 750-766. Thanks! (3 Replies)
Discussion started by: netrom
3 Replies

3. Shell Programming and Scripting

Replacing multiple spaces in flat file

Greetings all I have a delimited text file (the delimiter is ';') where certain fields consist of many blanks e.g. ; ; and ; ; Before I separate the data I need to eliminate these blanks altogether. I tried the sed command using the following syntax: sed -i 's/; *;/;;/g' <filename> ... (15 Replies)
Discussion started by: S. BASU
15 Replies

4. Shell Programming and Scripting

Preserve spaces while reading character character

Hi All, I am trying to read a file character by character, #!/bin/bash while read -n1 char; do echo -e "$char\c" done < /home/shak/testprogram/words Newyork is a very good city. Newyorkisaverygoodcityforliving I need to preserve the spaces as thats an... (3 Replies)
Discussion started by: Kingcobra
3 Replies

5. Shell Programming and Scripting

Replacing white spaces in filename

Hi; In following code find LOG_DIR -type f | while read filename; do echo $filename; done I want to precede each white space encountered in filename with \ so that when i use $filename for running some commands in do...done,it wont give me an error. will appreciate ur help in this.... (1 Reply)
Discussion started by: ajaypadvi
1 Replies

6. Shell Programming and Scripting

Replacing tabs with spaces

I want my program to replace tabs with spaces.1tab=4spaces.When i write aa(tab)aaa(tab)(tab)a(tab) it must show me aaxxaaaxxxxxaxxx. I think that my program works corectly but when a write aaa(tab)a it must show aaaxa but it is aaaxxxxxa.Please for help!!! That is my code: #include <stdio.h> ... (3 Replies)
Discussion started by: marto1914
3 Replies

7. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: badrimohanty
2 Replies

8. 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

9. 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

10. Shell Programming and Scripting

replacing tabs to spaces from files

hi, I have some 50 C files in which for indentation of code some devlopers used tabs, but we dont want any tab used for indentation. I have following 2 need. 1) find tabs from all 50 files (which are in one directory ) 2) replace them with 4 spaces. Thanks Rishi (6 Replies)
Discussion started by: rishir
6 Replies
Login or Register to Ask a Question