Ignore all lines except the --- dash line in a text file.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Ignore all lines except the --- dash line in a text file.
# 1  
Ignore all lines except the --- dash line in a text file.

How do you write a script to ignore all lines except the --- dash lines and then remove --- dashes from the data in a text file?

Also how do you separate data in a text file with a tab (for example, column1 (software) and column2 (date) ) ?

Here is my scripts : I am getting errors in both:

Code:
#!/bin/bash


#code 1  removes all lines except lines begin with --- dashes and then write the results out  to a file (foo2.txt)

while read -r line; do
    [[ "$line" = *( )---* ]] && continue
   echo  "$line" > foo2.txt
done < foo 

#Code 2 remove --- dash

ed -s foo2 <<-EOF
        \$-1s/ *||---*||//
        w
        q
EOF

foo.txt: Code 1 removes lines except for line begin with --- dashes . The contents in foo.txt are as follows:

Code:
1         the SAS System                    17:01 Tuesday, September 1, 2015
 

orginal validation 
site number:       zykbcds
expiration:       30019999
grace period:     45 days
warning period:    45 days
product expiration dates:


---B colb software 30Nov2015
---SG/software 30Oct2015 
---GH/Software 30Nov2015
---KP/software 30Oct2015
---lP/software 30Oct2015
rv analytic enterprise bnm evironment software /software28JAN2015



foo2.txt: Code 2 removes all dashes in the data and then add tab between columns . The contents in foo2.txt are as follows:

Code:
---B colb software                                                             30Nov2015
---SG/software                                                                 30Oct2015 
---GH/Software                                                                30Nov2015
---KP/software                                                                 30Oct2015
---lP/software                                                                  30Oct2015
---rv analytic enterprise bnm  evironment software /software  28JAN2015


foo2.txt: The final results of code 2. I need for the final file to look like the contents below

Code:
B colb software                                                              30Nov2015
SG/software                                                                   30Oct2015 
GH/Software                                                                  30Nov2015
KP/software                                                                   30Oct2015
lP/software                                                                    30Oct2015
rv analytic enterprise bnm  evironment software /software    28JAN2015


Last edited by Don Cragun; 09-06-2015 at 08:44 PM.. Reason: Add CODE tags.
# 2  
Use code tags to post code/console output as you have agreed by the forum rules.
That way, one would see the difference between tabs and spaces.

What is the error message?
Is it really required to parse for 3 dashes?

Stay with 1 sample input, code and expected output, then move to the next topic.
Makes it easier to follow which changes are required where to create the expected output.

Have a try/start with:
Code:
grep [sS]oftware foo | \
	while read -r tmp_data 
	do
		data="${tmp_data/---}"
		echo "Parsing: $data"
	done

hth

EDIT:
And your topic has nothing to do with this website....
# 3  
wouldn't it be easier (and perhaps faster) to use sed?

Code:
sed -n 's/^---//p' /path/to/input > /path/to/output

I hope this helps.

bakunin
# 4  
Extending bakunin's proposal to include the desired column separator:
Code:
sed -n 's/^---//;T; s/ \([[:alnum:]]*\) *$/\t\1/p' file
B colb software    30Nov2015
SG/software    30Oct2015
GH/Software    30Nov2015
KP/software    30Oct2015
lP/software    30Oct2015

 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #349
Difficulty: Easy
AWK was originally written in 1977 and distributed with Version 7 Unix.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

3. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

5. Shell Programming and Scripting

How to ignore single or multiple lines between /* and */ while reading from a file in unix?

I have a file proc.txt: if @debug = 1 then message 'Start Processing ', @procname, dateformat(now(*), 'hh:mm:ss'), @julian type info to client; end if; /* execute immediate with quotes 'insert into sys_suppdata (property, value, key_name) location ''' || @supp_server || '.' ||... (5 Replies)
Discussion started by: kidncute
5 Replies

6. Shell Programming and Scripting

awk : ignore first 5 line and last line of a file

Hi All, I have text file like as below temp.txt 1 Line temp.txt 2 Line temp.txt 3 Line temp.txt 4 Line temp.txt 5 Line temp.txt 6 Line temp.txt 7 Line temp.txt 8 Line temp.txt N Line I expect the output like as below processing 6 ... processing 7 ... (6 Replies)
Discussion started by: k_manimuthu
6 Replies

7. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

8. Shell Programming and Scripting

shellscript to find a line in between a particular set of lines of a text file

i have a file a.txt and following is only one portion. I want to search <branch value="/dev36/AREA/" include="yes"></branch> present in between <template_file name="Approve External" path="core/approve/bin" and </template_file> where the no of lines containing "<branch value= " is increasing ... (2 Replies)
Discussion started by: millan
2 Replies

9. Shell Programming and Scripting

How to print the lines after 2nd line from text file

Hi All, I need to print the lines after 2nd line irrespective of the number of lines in file if the file contents are like below ---------- root:!:0:0::/root:/usr/bin/ksh daemon:!:1:1::/etc: bin:!:2:2::/bin: sys:!:3:3::/usr/sys: adm:!:4:4::/var/adm: uucp:!:5:5::/usr/lib/uucp:... (6 Replies)
Discussion started by: ram1729
6 Replies

10. Shell Programming and Scripting

Ignore some lines with specific words from file comparison

Hi all, I need help in doing this scenario. I have two files with multiple lines. I want to compare these two files but ignoring the lines which have words like Tran, Loc, Addr, Charge. Also if i have a word Credit in line, i want to tokenize (i.e string after character " ... (2 Replies)
Discussion started by: jakSun8
2 Replies