Need help in scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in scripting
# 1  
Old 09-13-2017
Need help in scripting

Hi,

I am trying to find a script that will help me in replacing ^M if present in file with new line if new line is not present after ^M else leave the file as it is.

Can someone help me with the scripts.

I know it can be done using sed command but it is even changing the files which does not have ^M.
# 2  
Old 09-13-2017
So what have you tried so far?
Quote:
I know it can be done using sed command but it is even changing the files which does not have ^M.
If "^M" does not exist how can sed change anything? We need to see what you used to understand ...
# 3  
Old 09-13-2017
Code:
sed 's/'"$(printf '\015')"'/\
/g' $1 > $2

I am using above sed command.

The above command is changing the file which does not have ^M and putting a blank entry in between rows for all dat athat is present in file

Last edited by rajrishi990; 09-13-2017 at 04:20 AM.. Reason: added comment
# 4  
Old 09-13-2017
That's a bit difficult to believe - sed wouldn't substitute anything if no ^M char were present. At least it doesn't on my linux nor FreeBSD systems.
So - please post your OS and sed version, and a representative sample of your input file.
And, be aware of
- a ^M can be used in e.g. spread sheet programs to signify an intra-cell line break.
- a ^M can be used as a line terminator in certain systems.
- there are easier / more straightforward ways to remove the ^M char in text files.
# 5  
Old 09-13-2017
HI, My file data is as below:-
Code:
ID,Status,parentid,status_new,Locations,date
4373,Complete,41,Active,,
4374,Complete,41,Active,,
435,Complete,41,Active,,


when i use the command that i mentioned it is changing the file into like below: - One additional space is getting added at between each row
Code:
ID,Status,parentid,status_new,Locations,date

4373,Complete,41,Active,,

4374,Complete,41,Active,,

435,Complete,41,Active,,


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-13-2017 at 07:50 AM.. Reason: Added CODE tags.
# 6  
Old 09-13-2017
Well, I strongly believe that every single line has a ^M at line end, then. Your script is changing the ^M^J (<CR><LF>) sequence to ^J^J (<LF><LF>) causing the extra line feed...

Try sed 's/\015//' file or tr -d '\r' <file instead - if your system (which you fail to mention) provides either.
# 7  
Old 09-13-2017
that is not the case.
if my file had ^M in all csv files, i culd see that in UNIX when i use vi command but that is not the case.

The file data i gave earlier does not have any ^M characters.
I have other file which has data like below

Code:
ID,Status,Name^M10000026,Active,INC^M10000177,Inactive,APCONINC^M

so the command i gave works fine for this but not for the one i gave earlier.

FYI.. the command you gave is not working for the file data that i am giving in this response
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

2. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

3. Android

Android Scripting Environment: Shell Scripting and Android

I just upgraded to Android 2.2 from 2.1. The GPS issue that was troublesome in 2.1 seems to have been fixed. Some of web browsing seems faster, but it could just be my connection is better today ;) Flash works in some browsers but not very good and it is too slow for Flash apps designed for... (0 Replies)
Discussion started by: Neo
0 Replies

4. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

5. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

6. UNIX for Dummies Questions & Answers

Scripting Help

Hello all. Like Ive stated before, I am new to world of Unix. I was given the tast to create a script that will concatenate 20 files into one file. I know that the cat command is to be used but I am looking for something to get started on. Again, basically I need to wrtie a script that whill... (3 Replies)
Discussion started by: ndoggy020
3 Replies

7. UNIX for Advanced & Expert Users

Need help on scripting

in unix bc command is used as calculator and also for conversion, i want to convert 5f to decimal. but bc dont consider 5f as hex value it considers 5F as hex value. I get 5f from other iteration so i cant change that f to F...is there any way to convert 5f to decimal ot 5f to 5F :rolleyes: (2 Replies)
Discussion started by: abhinandantn
2 Replies

8. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

9. Shell Programming and Scripting

scripting guru's pls help me with scripting on AIX

can someone pls help me with the script for a files coming from one system to a particular directory and i want to write a script to move those files to another directory on different system by renaming the files... pls someone help me on this... thanking in anticipation.... (1 Reply)
Discussion started by: thatiprashant
1 Replies

10. AIX

New to scripting

We have a scripting requirement, Background: On a particular path, we compress a load of log files into tar.gz. This is done on an hourly basis throughout the day and it produces files of the following format, 2005-08-05-00-021031.tar.gz as an example. This is done by a script. We need: ... (2 Replies)
Discussion started by: rajesh_149
2 Replies
Login or Register to Ask a Question