Remove line break at specific position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove line break at specific position
# 8  
Old 08-13-2018
1) This is what I've tried so far:

Code:
sed ':a;N;$!ba;s/^\(.\{30\}\)\n/./g'

Code:
sed 's/^\(.\{30\}\)\n/./g'

Code:
awk '{ gsub(/\n/,"",$30); print $0}'

2) I don't consider that any line should have a CR at position 30
# 9  
Old 08-13-2018
I am looking at your third option (awk) but do not see anything obviously wrong. What is happening to your data with this approach?
One thing, in the back of my head, is that some files terminate <CR> while others terminate <CR><LF>. And the unix2dos and dos2unix commands.
Have you done a hexdump/octaldump of the file to see the actual characters?

There are a few threads on this here --
ascii to hex
# 10  
Old 08-13-2018
Assuming that there are no tab characters on any line (or that you count a tab character as always occupying one position) and that you want to keep CR/LF character pairs as line terminators, the following should work on Cygwin for your sample:
Code:
awk '
substr($0, 30, 1) == "\r" {
        printf("%29.29s", $0)
        next
}
1' file

On Solaris systems, you'll need to use /usr/xpg4/bin/awk or nawk instead of awk.

Note that the above code might not work on any system if your files have CR/LF line separators instead of line terminators AND it will not work on UNIX text files that have LF line terminators.
# 11  
Old 08-13-2018
I'm afraid they're not working:

Code:
##File with LF only or CR/LF at end of line:

$ awk '
substr($0, 30, 1) == "\n" {
        printf("%29.29s", $0)
        next
}
1' shorttest1.txt
this is ok
this line is divided at posit
ion 30. The same as this one,
also position 30
the rest of lines are ok
with different lengths
The longest ones are always s
plitted at same position

##->Output is same as file

Code:
##File with CR only at the end of each line

]$ awk '
substr($0, 30, 1) == "\r" {
        printf("%29.29s", $0)
        next
}
1' shorttest1.txt
plitted at same positionays s

##-> Output is a mix of two last lines

------ Post updated at 07:41 PM ------

It works if we look for character at position 29 (the one before the end of line):

Code:
$ awk '
substr($0, 29, 1) == "t" {
        printf("%29.29s", $0)
        next
}
1' shorttest1.txt
this is ok
this line is divided at position 30. The same as this one,
also position 30
the rest of lines are ok
with different lengths
The longest ones are always s
plitted at same position

But it's not a solution as I don't have any string that matches all lines
# 12  
Old 08-13-2018
In actual fact with the conditions you set, the <CR><LF> pair, Don's version works as predicted. You suggested CygWin so the odds are that text files will have line terminators of the above pair.
Don will be using a 2007 version of bash on OSX 10.13.6 and I am using the current Linux Mint version.
Code:
bazza@amiga-MacBookPro:~$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright © 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
bazza@amiga-MacBookPro:~$ cd ~/Desktop/Code/Shell
bazza@amiga-MacBookPro:~/Desktop/Code/Shell$ ./split_lines.sh
this is ok
this line is divided at position 30. The same as this one,also position 30
the rest of lines are ok
with different lengths
The longest ones are always splitted at same position
bazza@amiga-MacBookPro:~/Desktop/Code/Shell$ _

If you are now suggesting that there my be any one of the four line termination methods:
1) <CR>
2) <LF>
3) <CR><LF>
4) <LF><CR>
...then this is a completely different requirement.
EDIT:
What does the "t" denote?
Are we expecting "\t" tabs as well?

------ Post updated at 08:01 PM ------

Not sure if this adds to the end of my other post but here goes:
Using a modified version of Don's code for UNIX style newlines only:
Code:
awk '
substr($0, 29, 1) != "" {
        printf("%29.29s", $0)
        next
}
1' < /tmp/text

Results Linux Mint 19:
Code:
bazza@amiga-MacBookPro:~$ cd ~/Desktop/Code/Shell
bazza@amiga-MacBookPro:~/Desktop/Code/Shell$ ./split_lines.sh
this is ok
this line is divided at position 30. The same as this one,also position 30
the rest of lines are ok
with different lengths
The longest ones are always splitted at same position
bazza@amiga-MacBookPro:~/Desktop/Code/Shell$ _


Last edited by wisecracker; 08-13-2018 at 04:43 PM.. Reason: See above...
# 13  
Old 08-13-2018
Let's stop poking in the dark - data needed!



Quote:
Originally Posted by joeyg
. . .

Have you done a hexdump/octaldump of the file to see the actual characters?

. . .

From the top of my head - post the result of od -ctx1 yourinputfile so we can see what we're dealing with.
This User Gave Thanks to RudiC For This Post:
# 14  
Old 08-14-2018
Hi all,

First of all, sorry if I didn't explain myself properly. My original file has <CR><LF> as line termination pattern for every single line. But as Don suggested that it will not work with <LF> I removed them using tr:

Code:
cat shortest1.txt | tr -d "\n" > nolf.txt

And using dos2unix command, result file had only <LF> as line terminators. That's why I tried with three different formats.

My bash version:

Code:
$ bash --version
GNU bash, version 4.4.12(3)-release (i686-pc-cygwin)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

And finally, I have tried with wisecracker modification it works:

Code:
$ awk '
substr($0, 29, 1) != "" {
        printf("%29.29s", $0)
        next
}
1' shorttest1.txt
this is ok
this line is divided at position 30. The same as this one,also position 30
the rest of lines are ok
with different lengths
The longest ones are always splitted at same position

Thanks a lot to everyone!!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Need help on find and replacement on specific line and position

I have a file with 100 lines. On 50 th line , from position 5 to rest of the data , I need to change the occurrence of A to B and Occurrence of M to N. Input file : Line1 Line2 Line3 -- -- 12345ABCDEFM --- -- Line 100 Output Line1 Line2 (40 Replies)
Discussion started by: Rajesh_us
40 Replies

3. UNIX for Dummies Questions & Answers

Remove Line Break VI

I'm trying to make a script that says echo This is the date: I did this echo This is the date: date and it worked. But I need them both on the same line. And putting date on the echo line doesn't work. Is there a way to do so? (1 Reply)
Discussion started by: bbowers
1 Replies

4. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

5. Shell Programming and Scripting

How to remove line break character in a file

Hi, we are trying to process a csv file,in which we are getting data with line breaks.How to remove the line break character in the file? when i try to print the line break charcter using od -c,it gives as '\n' character for both line break and line feed. Please provide your valuable... (6 Replies)
Discussion started by: cnraja
6 Replies

6. Shell Programming and Scripting

Help to remove line break

My requirement is to read the csv file and need to remove if any line break in it. sample data: Row1: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91Row2:"Tønsberg, Brygga Kino SF",Tønsberg,202-1.Tønsberg SF 4,202-1-4 Expected data: Row1: "Oslo, Symra kino",Oslo,130-7,Symra... (4 Replies)
Discussion started by: cnraja
4 Replies

7. Shell Programming and Scripting

How to remove line break in a csv file

Hi Experts, My requirement is to read the csv file and need to remove if any line break in it. sample data: Row1: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 Row2:"Tønsberg, Brygga Kino SF",Tønsberg,202-1, Tønsberg SF 4,202-1-4 Expected data: Row1: "Oslo, Symra... (6 Replies)
Discussion started by: cnraja
6 Replies

8. Shell Programming and Scripting

Deleting Characters at specific position in a line if the line is certain length

I've got a file that would have lines similar to: 12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 23456781 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 45678123 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 xx.00... (10 Replies)
Discussion started by: Cailet
10 Replies

9. Shell Programming and Scripting

Remove Line Break

Dear all, Please advise what approach can remove all line break from a text file? e.g. Source file: A B C Target file: A, B, C Thanks, Rock (5 Replies)
Discussion started by: Rock
5 Replies
Login or Register to Ask a Question