Sponsored Content
Top Forums Shell Programming and Scripting Compare an item in one line of a file against an item in the next line of the same file Post 303045895 by championc on Friday 17th of April 2020 11:34:41 AM
Old 04-17-2020
Compare an item in one line of a file against an item in the next line of the same file

Hi all,

I found the following old thread very useful and in particular the very helpful post by @frans
Code:
while read A # read line and put in variable A. If unreadable (EOF) the break the loop.
do
    read B # read next line and put in variable B
    ((i+=2)) # increment variable i by 2 (like i=i+2)
    # [ "$A" != "$B" ] && echo "Lines $((i-1)) and $i are different"
    # can be written
    if [ "$A" != "$B" ] # if values of variables A and B are different ('[ ]' stands for 'test')
    then echo "Lines $((i-1)) and $i are different" # OK ?
    fi
done < inputfile # tells the loop to read from inputfile (else, it would read from keyboard.)

However, I am looking to try and compare one item / word from the first line to see if that same item is part of the second line

So in a sense, I'm trying to say something like
if $A | grep "XXX" = $B | grep "XXX" then do ..........

I know the position of the word in the first line so I tried using if [ "$A" | awk {'print $5'} = "$B" | awk {'print $2'} ]
but no joy. It didn't like the syntax of that.

Last edited by championc; 04-17-2020 at 01:29 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for file and stopping at first item found

Hello, I try to write a shell script that would list all files on a directory and stop when it finds the first item specified on a find or ls command. How can I tell to the find or ls command to stop when it finds the first ".doc" file for example ? Thank you (7 Replies)
Discussion started by: davchris
7 Replies

2. Shell Programming and Scripting

Reading each item from a formatted file

Hi, I have a file generated like this - 1. Fire SQL and store the formatted output in a temp file echo "select path, empid, age from emp_tbl" | /usr/sql emp_db 2 > count_file | grep vol > tempFile 2. The tempFile looks like this after the above statement /vol/emp1 0732 ... (9 Replies)
Discussion started by: angshuman_ag
9 Replies

3. Shell Programming and Scripting

Print new item in file with symbol

Dear all, I have encountered some problem here. I prompt the user for input and store it into a data file, eg. key in name and marks so the data file will look like this andrew 80 ben 75 and the next input is carine 90. So the problem here is i want to print... (2 Replies)
Discussion started by: branred
2 Replies

4. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

5. Shell Programming and Scripting

how to compare file line by line with awk

im a newbee to unix. I have a requirement to compare two files with awk. file1.txt a b c d e file2.txt a b d e here i want to compare each line in file1 with corresponding line in file2 and prinf the line with difference. ie to check required result as shown below a=a (dont... (3 Replies)
Discussion started by: kiranps
3 Replies

6. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

7. Shell Programming and Scripting

Read a lis, find items in a file from the list, change each item

Hello, I have some tab delimited text data, file: final_temp1 aname val NAME;r'(1,) 3.28584 r'(2,)<tab> NAME;r'(3,) 6.13003 NAME;r'(4,) 4.18037 r'(5,)<tab> You can see that the data is incomplete in some cases. There is a trailing tab after the first column for each incomplete row. I... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

8. Shell Programming and Scripting

How to compare previous and current item in for loop in bash?

Hey, I am trying to compare formated login and logout dates from one user at a host which I have stored in a tmp directory in order to find out the total login time. I need to compare them in order to find overlapping intervals. At first I tried to store each log in and logo date in an array... (3 Replies)
Discussion started by: Mumu123
3 Replies

9. UNIX for Beginners Questions & Answers

Zabbix item for last line of a log file

Dear all,Zabbix version : 2.4 (yes, I know, upgrading soon - honest) Server OS version : CentOS 6, 64-bit (CentOS 7 with the Zabbix upgrade)I've got a large log file that I would like to read by an external process. It's basically the same as reading the item value on a web-page. I have... (5 Replies)
Discussion started by: rbatte1
5 Replies

10. Shell Programming and Scripting

Script to process a list of items and uncomment lines with that item in a second file

Hello, I have a src code file where I need to uncomment many lines. The lines I need to uncomment look like, C CALL l_r(DESNAME,DESOUT, 'Gmax', ESH(10), NO_APP, JJ) The comment is the "C" in the first column. This needs to be deleted so that there are 6 spaces preceding "CALL".... (7 Replies)
Discussion started by: LMHmedchem
7 Replies
read(1)                                                            User Commands                                                           read(1)

NAME
read - read a line from standard input SYNOPSIS
/usr/bin/read [-r] var... sh read name... csh set variable = $< ksh read [ -prsu [n]] [ name ? prompt] [name...] DESCRIPTION
/usr/bin/read The read utility will read a single line from standard input. By default, unless the -r option is specified, backslash () acts as an escape character. If standard input is a terminal device and the invoking shell is interactive, read will prompt for a continuation line when: o The shell reads an input line ending with a backslash, unless the -r option is specified. o A here-document is not terminated after a NEWLINE character is entered. The line will be split into fields as in the shell. The first field will be assigned to the first variable var, the second field to the second variable var, and so forth. If there are fewer var operands specified than there are fields, the leftover fields and their interven- ing separators will be assigned to the last var. If there are fewer fields than vars, the remaining vars will be set to empty strings. The setting of variables specified by the var operands will affect the current shell execution environment. If it is called in a subshell or separate utility execution environment, such as one of the following: (read foo) nohup read ... find . -exec read ... ; it will not affect the shell variables in the caller's environment. The standard input must be a text file. sh One line is read from the standard input and, using the internal field separator, IFS (normally space or tab), to delimit word boundaries, the first word is assigned to the first name, the second word to the second name, and so on, with leftover words assigned to the last name. Lines can be continued using ewline. Characters other than NEWLINE can be quoted by preceding them with a backslash. These backslashes are removed before words are assigned to names, and no interpretation is done on the character that follows the backslash. The return code is 0, unless an end-of-file is encountered. csh The notation: set variable = $< loads one line of standard input as the value for variable. (See csh(1)). ksh The shell input mechanism. One line is read and is broken up into fields using the characters in IFS as separators. The escape character, (), is used to remove any special meaning for the next character and for line continuation. In raw mode, -r, the character is not treated specially. The first field is assigned to the first name, the second field to the second name, and so on, with leftover fields assigned to the last name. The -p option causes the input line to be taken from the input pipe of a process spawned by the shell using |&. If the -s flag is present, the input will be saved as a command in the history file. The flag -u can be used to specify a one digit file descriptor unit n to read from. The file descriptor can be opened with the exec special command. The default value of n is 0. If name is omitted, REPLY is used as the default name. The exit status is 0 unless the input file is not open for reading or an end-of-file is encoun- tered. An end-of-file with the -p option causes cleanup for this process so that another can be spawned. If the first argument contains a ?, the remainder of this word is used as a prompt on standard error when the shell is interactive. The exit status is 0 unless an end-of- file is encountered. OPTIONS
The following option is supported: -r Does not treat a backslash character in any special way. Considers each backslash to be part of the input line. OPERANDS
The following operand is supported: var The name of an existing or non-existing shell variable. EXAMPLES
Example 1: An example of the read command The following example for /usr/bin/read prints a file with the first field of each line moved to the end of the line: example% while read -r xx yy do printf "%s %s " "$yy" "$xx" done < input_file ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of read: LANG, LC_ALL, LC_CTYPE, LC_MES- SAGES, and NLSPATH. IFS Determines the internal field separators used to delimit fields. PS2 Provides the prompt string that an interactive shell will write to standard error when a line ending with a backslash is read and the -r option was not specified, or if a here-document is not terminated after a newline character is entered. EXIT STATUS
The following exit values are returned: 0 Successful completion. >0 End-of-file was detected or an error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), ksh(1), line(1), set(1), sh(1), attributes(5), environ(5), standards(5) SunOS 5.10 28 Mar 1995 read(1)
All times are GMT -4. The time now is 02:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy