Copying section of file based on search criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying section of file based on search criteria
# 8  
Old 04-24-2014
Hi Don,

Thanks for the update. I am running into a small issue where one line in the file is getting converted to lower case. How can I ignore a line to get converted to a lower case.

Code:
File1:
cn=abc
cn=DEF
cn=xyz

Code:
File2:
dn: cn=ABC,ou=111,dc=111,dc=111,dc=com
uid: ABC
Xid: abcDEF123dDFV
userPassword:: lQ0E9sPsdfQ==

dn: cn=ghi,ou=111,dc=111,dc=111,dc=com
uid: ghi
Xid: dfsf3242SDS
userPassword:: PZHc9PsdfQ==

dn: cn=xyz,ou=111,dc=111,dc=111,dc=com
Xid: rew32DSDFS234
uid: xyz
userPassword:: PZHc9PsdfQ==

dn: cn=mno,ou=111,dc=111,dc=111,dc=com
uid: mno
Xid: DFDre23SD
userPassword:: PZHc9P23Q==

dn: cn=pqr,ou=111,dc=111,dc=111,dc=com
uid: pqr
Xid: 54we3reWEdfse
userPassword:: PZHc9P23Q==

dn: cn=def,ou=111,dc=111,dc=111,dc=com
uid: def
userPassword:: PZHc9P23Q==

Xid: AB34dSCrevtT3


Code:
Output:
dn: cn=abc,ou=111,dc=111,dc=111,dc=com
uid: abc
Xid: abcDEF123dDFV
userPassword:: lQ0E9sPsdfQ==

dn: cn=xyz,ou=111,dc=111,dc=111,dc=com
Xid: rew32DSDFS234
uid: xyz
userPassword:: PZHc9PsdfQ==

dn: cn=def,ou=111,dc=111,dc=111,dc=com
uid: def
userPassword:: PZHc9P23Q==
Xid: AB34dSCrevtT3

The line starting with Xid: should not get converted to lower case.

Thanks,
Sam
# 9  
Old 04-25-2014
Quote:
Originally Posted by Samingla
Hi Don,

Thanks for the update. I am running into a small issue where one line in the file is getting converted to lower case. How can I ignore a line to get converted to a lower case.

Code:
File1:
cn=abc
cn=DEF
cn=xyz

Code:
File2:
dn: cn=ABC,ou=111,dc=111,dc=111,dc=com
uid: ABC
Xid: abcDEF123dDFV
userPassword:: lQ0E9sPsdfQ==

dn: cn=ghi,ou=111,dc=111,dc=111,dc=com
uid: ghi
Xid: dfsf3242SDS
userPassword:: PZHc9PsdfQ==

dn: cn=xyz,ou=111,dc=111,dc=111,dc=com
Xid: rew32DSDFS234
uid: xyz
userPassword:: PZHc9PsdfQ==

dn: cn=mno,ou=111,dc=111,dc=111,dc=com
uid: mno
Xid: DFDre23SD
userPassword:: PZHc9P23Q==

dn: cn=pqr,ou=111,dc=111,dc=111,dc=com
uid: pqr
Xid: 54we3reWEdfse
userPassword:: PZHc9P23Q==

dn: cn=def,ou=111,dc=111,dc=111,dc=com
uid: def
userPassword:: PZHc9P23Q==

Xid: AB34dSCrevtT3


Code:
Output:
dn: cn=abc,ou=111,dc=111,dc=111,dc=com
uid: abc
Xid: abcDEF123dDFV
userPassword:: lQ0E9sPsdfQ==

dn: cn=xyz,ou=111,dc=111,dc=111,dc=com
Xid: rew32DSDFS234
uid: xyz
userPassword:: PZHc9PsdfQ==

dn: cn=def,ou=111,dc=111,dc=111,dc=com
uid: def
userPassword:: PZHc9P23Q==
Xid: AB34dSCrevtT3

The line starting with Xid: should not get converted to lower case.

Thanks,
Sam
Hi Sam,In message #6 in this thread, you said that the output you wanted should be all lowercase. I ignored that request for the userPassword:: lines because I didn't expect password comparisons to work correctly if the (presumably) encrypted password had been converted to single case.

Can you find the line in the script I gave you:
Code:
awk -F '[ ,]' '
FNR == NR {
	cn[tolower($1)]
	next
}
$1 == "dn:" {
	copy = (tolower($2) in cn)
}
copy {	print ($1 == "userPassword::" ? $0 : tolower($0))
}' File1 File2

that translates the output to lowercase for every output line except lines that have userPassword:: in the first field on the line?

After looking at the man page for awk, what change do think would be needed to make that line of code convert all input lines except those that have userPassword:: and those that have Xid: in the first field to lowercase?
# 10  
Old 04-25-2014
Hi Don,

Thanks for your time. Below is the line of code that does not convert the user password to lower case.
Code:
copy {	print ($1 == "userPassword::" ? $0 : tolower($0))

I have to use a similar code in the script but I am not sure where to add that. I tried it by adding but all the lines were getting duplicated.
Code:
copy {	print ($1 == "Xid:" ? $0 : tolower($0))

Sam
# 11  
Old 04-25-2014
Quote:
Originally Posted by Samingla
Hi Don,

Thanks for your time. Below is the line of code that does not convert the user password to lower case.
Code:
copy {	print ($1 == "userPassword::" ? $0 : tolower($0))

I have to use a similar code in the script but I am not sure where to add that. I tried it by adding but all the lines were getting duplicated.
Code:
copy {	print ($1 == "Xid:" ? $0 : tolower($0))

Sam
Good, you're learning. You found the right line.

Instead of duplicating that line to look for Xid lines, try changing it to:
Code:
copy {  print ($1 == "userPassword::" || $1 == "Xid:" ? $0 : tolower($0))

Doing it this way, there is only one print statement for each line that you want to print. Lines with either of those two values in the first field ($1 == "userPassword::" || $1 == "Xid:") are printed as they appeared in the input file ($0) while all other lines are converted to lower case (tolower($0)) before being printed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match based on criteria to file

Trying to match $1 of target.txt to $5 of file.txt. If there is a match then in an output.txt file $1,$1 (row underneath),$6,$4,$7 from file.txt are printed on the same line as $1 of target.txt. The input is from excel and the output should be tab-deliminated. Thank you :). target.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Need To Delete Lines Based On Search Criteria

Hi All, I have following input file. I wish to retain those lines which match multiple search criteria. The search criteria is stored in a variable seperated from each other by comma(,). SEARCH_CRITERIA = "REJECT, DUPLICATE" Input File: ERROR,MYFILE_20130214_11387,9,37.75... (3 Replies)
Discussion started by: angshuman
3 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

Copying corresponding entries based on search

Hi Guru's I need some help to accomplish the below. I have two files, file 1 has ldap entries with one of its parameter cn. I have one more file with cn and corresponding row id. The out put should get the cn from file1 search for the cn in file2 and get the value of id and add the id entry in... (19 Replies)
Discussion started by: Samingla
19 Replies

5. Shell Programming and Scripting

Deleting a section based on search from other file

Hi Everyone, I need some help to accomplish the below. help is highly appriciated. I have a 45 mb file with ldap entries. Each user entry is separated by a string # entry-id: 1 and so on. Some of the entries has a string xyz: true. I want to delete the section if the user section has xyz: true... (6 Replies)
Discussion started by: Samingla
6 Replies

6. Shell Programming and Scripting

Extract data based on specific search criteria

I have a huge file (about 2 millions records) contains data separated by “,” (comma). As part of the requirement, I can't change the format. The objective is to remove some of the records with the following condition. If the 23rd field on each line start with 302 , I need to remove that from the... (4 Replies)
Discussion started by: jaygamini
4 Replies

7. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

8. Shell Programming and Scripting

Delete new lines based on search criteria

Hi all! A bit of background: I am trying to create a script that formats SQL statements. I have gotten so far as to add new lines based on certain match criteria like commas, keywords etc. In the process, I end up adding newlines where I don't want. For example: substr(colName, 1, 10)... (3 Replies)
Discussion started by: jayarkay
3 Replies

9. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

10. UNIX for Dummies Questions & Answers

Select records based on search criteria on first column

Hi All, I need to select only those records having a non zero record in the first column of a comma delimited file. Suppose my input file is having data like: "0","01/08/2005 07:11:15",1,1,"Created",,"01/08/2005" "0","01/08/2005 07:12:40",1,1,"Created",,"01/08/2005"... (2 Replies)
Discussion started by: shashi_kiran_v
2 Replies
Login or Register to Ask a Question