awk sed to repeat every character on same position from the upper line replacing whitespace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk sed to repeat every character on same position from the upper line replacing whitespace
# 1  
Old 10-31-2017
awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position?

I am asking this because the file I have doesn't always follow a pattern.
For example the file I have is the result of a command to obtain windows ACLs:
icacls C:\ /t >output.txt


The order, number of lines that start with whitespace but have some text will change.
Code:
t e s t string 1 2 3 4 5
        sample a b c d e
        othersample 1 2 3 4 5

Should say

t e s t string 1 2 3 4 5
t e s t sample a b c d e
t e s t othersample 1 2 3 4 5

Another example: 
text 1 string 9 7 8 6
       something x \ / x
       something else 1 1 1 1 1 1 1 1

Same as before
text 1 string 9 7 8 6
text 1 something x \ / x
text 1 something else 1 1 1 1 1 1 1 1


Last edited by nakaedu; 10-31-2017 at 03:16 AM..
# 2  
Old 10-31-2017
Quote:
Originally Posted by nakaedu
Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position?

I am asking this because the file I have doesn't always follow a pattern.
For example the file I have is the result of a command to obtain windows ACLs:
icacls C:\ /t >output.txt


The order, number of lines that start with whitespace but have some text will change.
Code:
t e s t string 1 2 3 4 5
        sample a b c d e
        othersample 1 2 3 4 5

Should say

t e s t string 1 2 3 4 5
t e s t sample a b c d e
t e s t othersample 1 2 3 4 5

Another example: 
text 1 string 9 7 8 6
       something x \ / x
       something else 1 1 1 1 1 1 1 1

Same as before
text 1 string 9 7 8 6
text 1 something x \ / x
text 1 something else 1 1 1 1 1 1 1 1

I don't get it, why shouldn't the output be:

Code:
t e s t string 1 2 3 4 5
t e s t sample a b c d e
t e s t othersamplec1d2e3 4 5

and:
Code:
text 1 string 9 7 8 6
text 1 something7x8\6/ x
text 1 something7else/1x1 1 1 1 1 1 1

You say "whitespace". Are there tabs in the input you're processing or just spaces? If there are tabs, are they to be treated as a single whitespace character, or are they to be treated as one to eight space characters (assuming tab stops are set on every 8th position) depending on where they are located on an input line?

What operating system and shell are you using?

What have you tried to solve this problem on your own?
# 3  
Old 10-31-2017
Quote:
Originally Posted by Don Cragun
I don't get it, why shouldn't the output be:

Code:
t e s t string 1 2 3 4 5
t e s t sample a b c d e
t e s t othersamplec1d2e3 4 5

and:
Code:
text 1 string 9 7 8 6
text 1 something7x8\6/ x
text 1 something7else/1x1 1 1 1 1 1 1

I only need the text/string until there is a character that is not a whitespace on that line. If there is already data in there I do not want to modify that.
Quote:
You say "whitespace". Are there tabs in the input you're processing or just spaces? If there are tabs, are they to be treated as a single whitespace character, or are they to be treated as one to eight space characters (assuming tab stops are set on every 8th position) depending on where they are located on an input line?
I am afraid there is no tab, only spaces.

Quote:
What operating system and shell are you using?
I am on Windows using awk and sed

Quote:
What have you tried to solve this problem on your own?
I tried to obtain the leading whitespaces:
awk -F'[^ \t]' '{print length($1)}' now I am trying to obtain that number of characters from the line above where I find whitespaces
# 4  
Old 10-31-2017
Building on what you had come up with, try:
Code:
awk -F'[^ ]' '
{	current_len = length($1)
	last_len = length(last_line)
	len = current_len > last_len ? last_len : current_len
	last_line = substr(last_line, 1, len) substr($0, len + 1)
	print last_line
}' file

Note that this code resets itself when it sees an empty line. So, with the following in a file named file:
Code:
t e s t string 1 2 3 4 5
        sample a b c d e
        othersample 1 2 3 4 5

text 1 string 9 7 8 6
       something x \ / x
                 else 1 1 1 1 1 1 1 1

the above code produces the output:
Code:
t e s t string 1 2 3 4 5
t e s t sample a b c d e
t e s t othersample 1 2 3 4 5

text 1 string 9 7 8 6
text 1 something x \ / x
text 1 something else 1 1 1 1 1 1 1 1

which I think matches what you want.

If someone else wants to try this on a Solaris/SunOS system, change awk to nawk or /usr/xpg4/bin/awk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-31-2017
Code:
awk -F'[^ ]' '
{	current_len = length($1)
	last_len = length(last_line)
	len = current_len > last_len ? last_len : current_len
	last_line = substr(last_line, 1, len) substr($0, len + 1)
	print last_line
}' file

Thanks a lot, it worked perfectly.
I saved it as an awk file as I am on Windows to avoid the conversion/adaptation and I am launching as a .awk file and it works perfectly

Last edited by Don Cragun; 10-31-2017 at 07:12 PM.. Reason: Change QUOTE tags to CODE tags.
# 6  
Old 11-01-2017
You may want to try
Code:
awk '
1 == match ($0, "[^     ]")     {ORG = $0
                                }
                                {IX = index ($0, "      ")
                                 IX = RSTART + (IX?8:0) - IX
                                 print substr (ORG, 1, IX - 1) substr ($0, RSTART) 
                                }
' file

as well. It behaves slightly different from Don Cragun's proposal, but, if need be, can be easily adapted to behave the same. It doesn't need an empty line to recognize a new pattern, though.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

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

3. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

4. Shell Programming and Scripting

Repeat line convert upper line

HI Guys, My Input :- R3 AV44 50 0 100 100 100 R3 AV44 1 0 100 100 100 R3 AV45 50 0 100 100 100 R3 AV45 0 3 100 100 100 R3 AV45S 50 0 100 100 100 R3 AV45S 0 4 100 100 100 Output :- R3 AV44 50 0 100 100 100 1 0 100 100 100 R3 AV45 50 0 100 100 100 0 3 100 100 100 R3 AV45S 50 0... (9 Replies)
Discussion started by: pareshkp
9 Replies

5. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

6. Shell Programming and Scripting

position of character in a line

i want to find the position of a character in a line , the first position, last, 5th occurence position , i ve tried grep -n , and expr index but they dont fit the bill. Please let me know if there is any other alternative (2 Replies)
Discussion started by: phpsnook
2 Replies

7. Shell Programming and Scripting

Delete character in determinate position with sed/awk

Hello. I'm trying to delete one character in determinate position. Example: qwEtsdf123Ecv34 <delete character in positión 3> Result: qwtsdf123Ecv34 Plase, help me. Thanks (4 Replies)
Discussion started by: maria_florencia
4 Replies

8. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. UNIX for Dummies Questions & Answers

SED: Can't Repeat Search Character in SED Output

I'm not sure if the problem I'm seeing is an artifact of sed or simply a beginner's mistake. Here's the problem: I want to add a zero-width space following each underscore between XML tags. For example, if I had the following xml: <MY_BIG_TAG>This_is_a_test</MY_BIG_TAG> It should look like... (8 Replies)
Discussion started by: rhetoric101
8 Replies

10. Shell Programming and Scripting

error while replacing a string by new line character in sed

hi, when i am doing the following things getting error Can anyone please suggest i have a file where there is a line like the following branch=dev sdf dev jin kilii fin kale boyle dev james dev i want to search the existance of dev in the above line. cat "$file" | sed -n... (8 Replies)
Discussion started by: millan
8 Replies
Login or Register to Ask a Question