Help with text processing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with text processing
# 1  
Old 02-25-2015
Help with text processing

I have an Input file which has a series of lines(which could vary) followed by two blank lines and then another series of lines(Could be any number of lines) followed by two blank lines and then repeats. I need to use filters to convert the following input file(which is an example) to an output file as shown below. The output should be - first three lines separated by a comma character or "|" character and then the rest of lines should be appended as shown below in the output.

Input file:

Code:
a
b
c
d
e
f


p
q
r
s
t
u
v
w
x


Output :

Code:
a|b|c|d
a|b|c|e
a|b|c|f
p|q|r|s
p|q|r|t
p|q|r|u
p|q|r|v
p|q|r|w
p|q|r|x

Appreciate your time and interest. Thank you,
BikerBoy

Last edited by bartus11; 02-25-2015 at 06:11 PM.. Reason: Please use [code][/code] tags.
# 2  
Old 02-25-2015
Is this a homework assignment? If so, please refile in the Homework & Coursework Questions forum following the directions specified here.

If not, what have you tried?

Can there be single blank lines? If so, how are they to be handled?

Can there be more than two adjacent blank lines? If so, how are they to be handled?

What is supposed to happen if there are less than four lines between sets of blank lines?
# 3  
Old 02-25-2015
Thank you for the reply Don.

This is not a homework assignment.

I have tried using a couple of awk and sed filters but the effort went in vain.

There are always two blank lines. Not less or more.

There are always more than 4 or more lines in between two blank lines.
# 4  
Old 02-25-2015
Try:
Code:
awk '
NF == 0 {
	c = 0
	out = ""
	next
}
c++ < 3 {	
	out = out $1 "|"
	next
}
{	print out $1
}' file

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 02-25-2015
Super!. Big thanks to you Sir!!!.

One last question before I close this thread. What if the input is like this:

hi
this
is
biker boy
i like bikes
i love bikes


this
is
a
great forum
with
helpful people around
having immense knowledge
in unix commands
and scripting.


Output :
hi|this|is|biker boy
hi|this|is|i like bikes
hi|this|is|i love bikes


this|is|a|great forum
this|is|a|with
this|is|a|helpful people around
this|is|a|having immense knowledge
this|is|a|in unix commands
this|is|a|and scripting.


I really appreciate your effort.
Thank you.

---------- Post updated at 08:19 PM ---------- Previous update was at 08:17 PM ----------

Actually never mind. I figured it out.

Thank you so much!!!
# 6  
Old 02-25-2015
I'm glad that you figured it out.

Note that if the sample input you provide is more representative of the data you'll be processing, you'll get better suggestions for ways to get the output you want.

To help others who may read this thread in the future, it would be nice if you would post the script that you used to solve your expanded requirements.
# 7  
Old 02-25-2015
Noted!. This is my first post so I am learning to follow the rules and regulations of this forum.

The syntax I ran to execute the second input format is :

Code:
awk '
NF == 0 {
c = 0
out = ""
next
}
c++ < 3 {
out = out $n "|"
next
}
{print out $n}' file.txt

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code.

Last edited by Don Cragun; 02-25-2015 at 10:27 PM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Text processing in UNIX

Greetings! I have a text file that I am trying to process to get the desired output but looks like I will need the community help. Input File: a|x|london|consumer|consumer1|country||D|consumer|consumer1|country||1 a|x|paris|consumer|consumer2|country||D|consumer1|consumer2|country||2... (3 Replies)
Discussion started by: bikerboy
3 Replies

2. Shell Programming and Scripting

awk for text processing

Hi,my file is in this format ", \"symbol\": \"Rbm38\" } ]" I want to convert it to a more user readable format _id pubmed text symbol 67196 18667844 Overexpression of UBE2T in NIH3T3 cells significantly promoted colony formation in mouse cell cultures Ube2t 56190 21764855 ... (3 Replies)
Discussion started by: biofreek
3 Replies

3. Shell Programming and Scripting

Text processing

Hi, Need an advise on $ cat test.txt START field1 field2 field3 field4 field5 field6 END 12345|6|1|2|3|4|111|119 67890|6|1|3|8|9|112|000 $ (4 Replies)
Discussion started by: getmilo
4 Replies

4. Shell Programming and Scripting

Text processing using awk

I dispose of two tab-delimited files (the first column is the primary key): File 1 (there are multiple rows sharing the same key, I cannot merge them) A 28,29,30,31 A 17,18,19 B 11,13,14,15 B 8,9File 2 (there is one only row beginning with a given key) A 2,8,18,30,31 B ... (3 Replies)
Discussion started by: dovah
3 Replies

5. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

6. Shell Programming and Scripting

Text processing doubt

How to print nth column of a pattern/file without using awk,cut commands? (1 Reply)
Discussion started by: rajkumarin
1 Replies

7. Shell Programming and Scripting

Awk text processing

Hi Very much appreciate if somebody could give me a clue .. I undestand that it could be done with awk but have a limited experience. I have the following text in the file 1 909 YES NO 2 500 No NO . ... 1 ... (8 Replies)
Discussion started by: zam
8 Replies

8. Shell Programming and Scripting

seeking help in text processing

Hi, I am a newbie in shell scripting. I want to get an expert help in solving a text processing issue. The issue I am facing is that, in the below log file contents I need to extract each block of lines (it could be a single line also) based on some regular expression and store it in... (8 Replies)
Discussion started by: Alecs
8 Replies

9. UNIX for Dummies Questions & Answers

text file processing

Hello! There is a text file, that contains hierarchy of menues, like: Aaaaa->Bbbbb Aaaaa->Cccc Aaaaa-> {spaces} Ddddd (it means that the full path is Aaaaa->Cccc->Ddddd ) Aaaaa-> {more spaces} Eeeee (it means that the full path is Aaaaa->Cccc->Ddddd->Eeeee ) Fffffff->Ggggg... (1 Reply)
Discussion started by: alias47
1 Replies

10. UNIX for Dummies Questions & Answers

Processing a text file

A file contains one name per line, such as: john doe jack bruce nancy smith sam riley When I 'cat' the file, the white space is treated as a new line. For example list=`(cat /path/to/file.txt)` for items in $list do echo $items done I get: john doe (1 Reply)
Discussion started by: TheCrunge
1 Replies
Login or Register to Ask a Question