AWK/SED: handle max chars in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK/SED: handle max chars in a line
# 1  
Old 04-19-2011
MySQL AWK/SED: linebreak issue

Hi all,

I hope you guys can help me.
I prefer SED/AWK solutions if possible. For my shame it didn't work for me Smilie
ISSUE: Smilie
Code:
1\3

1/$4\@7\
1234567890123456789\
1234567890123456789,\
1234567890123456789\
123456789012
12345
1234567890123456789\
1234567890123456789,\
1234

Information:
If a line has more than 19 chars, it uses a "\" as 20th char and proceeds in next line.
This is present as you see.

Task:
I changed lines, therefore they do no longer fit into the 20 chars.
E.g. i add a ",". I want to put it into the next line and correct here the 20 chars rule and this for all following lines. Until it's no longer needed.

Solution:Smilie
Code:
1\3

1/$4\@7\
1234567890123456789\
1234567890123456789\
,123456789012345678\
9123456789012
12345
1234567890123456789\
1234567890123456789\
,1234

Thanks a lot!!! Smilie
_______________________
Tried things in style of:
One solution: Use two vars saving the lines... removing additional , saving this string and writing it in front of the next line holding var. Than use again previous var and read this. Until line has <19 char.
Shouldn't be that hard... but i'm not familiar with the syntax Smilie
______
length > 20...
awk '/.../ use_vars=$0; if(line){print line ORS $0; line=""}}' file

Last edited by unknown7; 04-20-2011 at 06:11 AM.. Reason: reformated and changed topic | added "\" into code tags to ensure understanding of the issue
# 2  
Old 04-19-2011
See if this works for you:
Code:
#!/usr/bin/ksh
typeset -i mLen
mPrevLine=''
sed 's/\\//' Inp_File | while read mLine
do
  mLine2=${mPrevLine}${mLine}
  mLen=${#mLine2}
  if [[ ${mLen} -ge 20 ]]; then
    while [[ ${mLen} -ge 20 ]]; do
      mOutLine=$(echo ${mLine2} | cut -c1-19)
      echo ${mOutLine}'\'
      mLine2=$(echo ${mLine2} | cut -c20-)
      mLen=${#mLine2}
      mPrevLine=${mLine2}
    done
  else
    echo ${mLine2}
    mPrevLine=''
  fi
done

PS: According to your rule, your solution should not have a "\" on the following line:
Code:
...
1234567890123456789\ <=====
1234567890123456789\
1234

This User Gave Thanks to Shell_Life For This Post:
# 3  
Old 04-19-2011
Quote:
Originally Posted by Shell_Life
PS: According to your rule, your solution should not have a "\" on the following line:
Code:
...
1234567890123456789\ <=====
1234567890123456789\
1234

Thanks a lot I will try.
Maybe you missunderstood me, I'm sorry if i explained to bad.
The ISSUE code was previousely done with this rule. It means whereever a "\" is, is NO newline. So its a line break character.
e.G. for linebreak after 5 chars:
Code:
wordAwordBwordC

is changed automatically to:

wordA\
wordB\
wordC

I added now a string to wordB:

wordA\
wordB_ADD\
wordC

should now result in following (I can't run the automatic tool again):

wordA\
wordB\
_ADDw\
ordC

So i'll check this and let u know if it worked... thanks so far!
# 4  
Old 04-19-2011
Code:
cat input.txt | awk '{
if (length($0)+length(APPEND) < 20 ){
 TMP = APPEND $0
 gsub(/\\/,"",TMP)
 print TMP
 APPEND=""
}else{
 
 TMP = APPEND $0 
 gsub(/\\/,"",TMP)
 LINE = substr(TMP,1,19)
 APPEND = substr(TMP,20)
 print LINE "\\"

}
 
}'

try this
This User Gave Thanks to |UVI| For This Post:
# 5  
Old 04-20-2011
@Shell life
EDIT: Solution nearly works. Thanks a lot. Just the first "\" is wrongly removed. If 19 char + "\" than do nothing. Means it was a longer char which was splitted prev. correctly. And do not remove "\" within lines.

NewTestcode:
Just remove "\" if it's last char in line and if line has >20 chars.
Remember the next line holds also the additional chars which were cut regarding to this rule!

Code:
AB!"§$&\\/()=?@\.*'A
12345678901234567890
1234567890123456789\
1234567890123456789,\
1234567890123456789\
123456789012
1234567890123456789\
1234567890123456789,\
A\B/C@D

newline_possible\/()

Result should be:
Code:
AB!"§$&\\/()=?@\.*'A
12345678901234567890
1234567890123456789\
1234567890123456789\
,123456789012345678\
9123456789012
1234567890123456789\
1234567890123456789\
,A\B/C@D

newline_possible\/()

If line is like:
12345678901234567890
and no "\" at the end -> do nothing.


Only if the last char of the 20 is a "\" than it's a splittet line. If there are 20 chars w/o a "\" nothing should be changed.
So 20 chars with "\" at the end means it was previously smth. bigger which was splitted.

So the "\" should just be removed if the line.length > 20 & the last char is a "\".
Tried to remove or move the sed command but i dont know how to let the loop run w/o sed ;-)

@UVI
Unfortunately it says syntax error... couldn't find the reson... could u recheck this please?

Thanks a lot!

---------- Post updated 20th Apr 2011 at 10:00 AM ---------- Previous update was 19th Apr 2011 at 07:03 PM ----------

Hmmm i don't get it...
additional check like: ${mline2: -1} -eq '\'
or with tail command

so much options... but my syntax is wrong :-/
Help is needed - thanks

Last edited by unknown7; 04-20-2011 at 06:03 AM..
# 6  
Old 04-20-2011
Please, first verify if you do not have in your input file the octal 251 which is ©.

If you do, then choose another special character that is not in your file.

Code:
#!/usr/bin/ksh
typeset -i mLen
mPrevLine=''
tr '\\' '\251' < Inp_File | while read mLine
do
  mLine2=${mPrevLine}${mLine}
  mLen=${#mLine2}
  if [[ ${mLen} -gt 20 ]]; then
    mLine2=$(echo ${mLine2} | sed 's/.$//')
    while [[ ${mLen} -gt 20 ]]; do
      mOutLine=$(echo ${mLine2} | cut -c1-19)
      echo ${mOutLine}'\'
      mLine2=$(echo ${mLine2} | cut -c20-)
      mLen=${#mLine2}
      mPrevLine=${mLine2}
    done
  else
    echo ${mLine2}
    mPrevLine=''
  fi
done | tr '\251' '\\'

This User Gave Thanks to Shell_Life For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep with special chars line by line

I have a file that includes strings with special characters, eg file1 line: 1 - special 1 line: = 4 line; -3 etc How can I grep the lines of file1 from file2, line by line? I used fgrep and egrep to grep a particular line and worked fine, but when I used: cat file1|while read line;do... (2 Replies)
Discussion started by: FelipeAd
2 Replies

2. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

3. UNIX for Dummies Questions & Answers

Using awk to find max and printing entire line

Hi folks, I am very new to awk. I have what is probably a very simple question. I'm trying to get the max value of column 1, but also print column 2. My data looks like this: 0.044|2000-02-03 14:00:00 5.23|2000-02-03 05:45:00 5.26|2000-02-03 11:15:00 0|2000-02-01 18:30:00 So in this case... (2 Replies)
Discussion started by: amandarobe
2 Replies

4. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

5. Shell Programming and Scripting

sed/awk to retrieve max year in column

I am trying to retrieve that max 'year' in a text file that is delimited by tilde (~). It is the second column and the values may be in Char format (double quoted) and have duplicate values. Please help. (4 Replies)
Discussion started by: CKT_newbie88
4 Replies

6. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

7. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies

8. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

9. Shell Programming and Scripting

Read logline line by line with awk/sed

Hello, I have a logfile which is in this format: 1211667249500#3265 1211667266687#2875 1211667270781#1828 Is there a way to read the logfile line by line every time I execute the code and put the two numbers in the line in two separate variables? Something like: 1211667249500#3265... (7 Replies)
Discussion started by: dejavu88
7 Replies

10. Shell Programming and Scripting

how to handle variables in sed

while read line do str1=`grep 'customernumberR11' file1 | cut -c 20-21` str2=`grep 'newnumberR11' file1 | cut -c 15-16` if ; then sed -e "s/newnumberR11/ s/$str2/\$str1/g" $line fi done < file1 (4 Replies)
Discussion started by: LAKSHMI NARAYAN
4 Replies
Login or Register to Ask a Question