Select only certain lines from file and mantain formatting


 
Thread Tools Search this Thread
# 1  
Select only certain lines from file and mantain formatting

I want to take the below data, and have it output to file only the STMC#/(IP address) and the "there are X number of updates to install" lines for each machine. I know it's easy, but Im a beginner in BASH stuff, my solution would probably take way too many lines to do something easy.Thanks!


STMC221 (192.168.171.76)
Password:
2008-10-30 14:38:49.495 softwareupdate[360] Loading CatalogURL http://mysus.company.lan:8088/index.sucatalog
No new software available.
There are no updates to install


STMC223 (192.168.23.25)
Password:
2008-10-30 14:38:29.245 softwareupdate[11526] Loading CatalogURL http://mysus.company.lan:8088/index.sucatalog
There are 1 updates to install


STMC225 (192.168.171.42)
Password:
2008-10-30 14:38:30.813 softwareupdate[3084] Loading CatalogURL http://mysus.company.lan:8088/index.sucatalog
There are 1 updates to install


STMC300 (192.168.171.240)
Password:
2008-10-30 14:38:31.661 softwareupdate[9255] Loading CatalogURL http://mysus.company.lan:8088/index.sucatalog
No new software available.
There are no updates to install
# 2  
Hammer & Screwdriver Here is one way

Code:
> cat file21
STMC221 (192.168.171.76)
Password:
2008-10-30 14:38:49.495 softwareupdate[360] Loading CatalogURL http://mysus.company.lan:8088/index.sucatalog
No new software available.
There are no updates to install


STMC223 (192.168.23.25)
Password:
2008-10-30 14:38:29.245 softwareupdate[11526] Loading CatalogURL http://mysus.company.lan:8088/index.sucatalog
There are 1 updates to install


STMC225 (192.168.171.42)
Password:
2008-10-30 14:38:30.813 softwareupdate[3084] Loading CatalogURL http://mysus.company.lan:8088/index.sucatalog
There are 1 updates to install


STMC300 (192.168.171.240)
Password:
2008-10-30 14:38:31.661 softwareupdate[9255] Loading CatalogURL http://mysus.company.lan:8088/index.sucatalog
No new software available.
There are no updates to install

Code:
> sed "s/^STM/~STM/" file21 | tr "\n" "|" | tr "~" "\n" | cut -d"|" -f1,4

STMC221 (192.168.171.76)|No new software available.
STMC223 (192.168.23.25)|There are 1 updates to install
STMC225 (192.168.171.42)|There are 1 updates to install
STMC300 (192.168.171.240)|No new software available.
>

# 3  
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk -F'\n' '{print$1,$NF}' RS= infile

With Perl:

Code:
perl -00 -F'\n' -ane'print"$F[0] $F[-1]\n"' infile


Last edited by radoulov; 10-30-2008 at 05:51 PM..
# 4  
Thanks everyone! I know it's not your problem, but since this is the beginners section, do you think you could explain your answers a little bit? Of course I will read up on it but it helps to have a head start.
# 5  
Hammer & Screwdriver

Code:
> sed "s/^STM/~STM/" file21 | tr "\n" "|" | tr "~" "\n" | cut -d"|" -f1,4

sed "s/^STM/~STM/" file21
   sed is an editor, file21 is my input
   I sustitute STM, where the ^ says at beginning of line
   with ~STM, so I can grab at that ~ later

|
   that is often called a pipe, and is not an l or L
   takes one result, and pipe's to next set of commands

tr "\n" "|"
   translate new-line characters to a | character
   I will want to use this as a delimiter or marker later

tr "~" "\n"
   translate the ~ to a new-line
   this effectively puts each instance on one line (was 4-5 lines at start)

cut -d"|" -f1,4
   using the | as a delimiter, as I put there earlier
   grab the 1st and 4th fields

Try the command yourself, just doing the first part. Then do again including the second part, and you will start to understand how data can be managed.
# 6  
Thanks Joey, that is VERY helpful!
 

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

Test Your Knowledge in Computers #59
Difficulty: Easy
A compiler is a software utility that translates a program written in high-level language into machine-level language.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Select only the lines of a file starting with a field which is matcing a list. awk?

Hello I have a large file1 which has many events like "2014010420" and following lines under each event that start with text . It has this form: 2014010420 num --- --- num .... NTE num num --- num... EFA num num --- num ... LASW num num --- num... (9 Replies)
Discussion started by: phaethon
9 Replies

2. Shell Programming and Scripting

awk - mixed for and if to select particular lines in a data file

Hi all, I am new to AWK and I am trying to solve a problem that is probably easy for an expert. Suppose I have the following data file input.txt: 20 35 43 20 23 54 20 62 21 20.5 43 12 20.5 33 11 20.5 89 87 21 33 20 21 22 21 21 56 87 I want to select from all lines having the... (4 Replies)
Discussion started by: naska
4 Replies

3. Shell Programming and Scripting

Select lines from a file based on a criteria

Hi I need to select lines from a txt file, I have got a line starting with ZMIO:MSISDN= and after a few line I have another line starting with 'MOBILE STATION ISDN NUMBER' and another one starting with 'VLR-ADDRESS' I need to copy these three lines as three different columns in a separate... (3 Replies)
Discussion started by: Tlcm sam
3 Replies

4. Shell Programming and Scripting

Short program to select lines from a file based on a second file

Hello, I use UBUNTU 12.04. I want to write a short program using awk to select some lines in a file based on a second file. My first file has this format with about 400,000 lines and 47 fields: SNP1 1 12.1 SNP2 1 13.2 SNP3 1 45.2 SNP4 1 23.4 My second file has this format: SNP2 SNP3... (1 Reply)
Discussion started by: Homa
1 Replies

5. UNIX for Dummies Questions & Answers

How to randomly select lines from a text file

I have a text file with 1000 lines, I want to randomly select 200 lines from it and print them as output. How do I go about doing that? Thanks! (7 Replies)
Discussion started by: evelibertine
7 Replies

6. UNIX for Dummies Questions & Answers

how to select lines from one file based on another file

Hi, I would like to know how can I select lines of one file based on a common ID column from another file (keeping the order of the second file). Example of file1: ID A B C D 1-30 1 2 3 5-60 4 5 6 1-20 7 8 9 Example of file2: ID chr pos 1-20 1 20 1-30 1 30 5-60 5 60 Desired... (2 Replies)
Discussion started by: fadista
2 Replies

7. Shell Programming and Scripting

sql select command output formatting in shell script

Hi, I need to connect to the database and retrieve two variables from the database and store them in a variable,out of these two variables I need to get lastdigit appended to the variable 1 retrieved and variable 2 with out any modification in short select var,data from usage; o/p=... (1 Reply)
Discussion started by: rkrish
1 Replies

8. Shell Programming and Scripting

Select lines in which column have value greater than some percent of total file lines

i have a file in following format 1 32 3 4 6 4 4 45 1 45 4 61 54 66 4 5 65 51 56 65 1 12 32 85 now here the total number of lines are 8(they vary each time) Now i want to select only those lines in which the values... (6 Replies)
Discussion started by: vaibhavkorde
6 Replies

9. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

10. Shell Programming and Scripting

formatting the sql select result

Hi, I have about 12 columns and 15 rows to be retrived from sybase isql command through unix. But when i output the sql into a file and see it, the formatting is going for a toss. can someone please suggest how can i get the result correctly in the output file ? Thanks, Sateesh (2 Replies)
Discussion started by: kotasateesh
2 Replies

Featured Tech Videos