sed script to print a value from txt file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
sed script to print a value from txt file

Hello guys,

I would appreciate if someone can help me to write a shell script using sed. From a larget text file I need to print a fixed value of a word. In another words whenever it finds that word, it needs to grab the other line containing "dn" and prints its value. For example:

dn: uid=poi.1111,ou=abc,o=cc.com
authpassword;orclcommonpwd: {SHA}adhnbckhsypzx=
businesscategory: abc
createtimestamp: 20050709198316x
creatorsname: cn=bulkload
modifiersname: cn=bulkload
modifytimestamp: 20050709198316x
objectclass: top
objectclass: cuser
cquestion: xyf88561

--------------------

in this example txt there are thousands of similar ones.

I need to be able to print "cquestion" and related "dn" for it using sed in a shell script. I would appreciate if someone can help me and email at "cmontr@verizon.com"

Thank you very much
# 2  
Quote:
Originally Posted by cmontr
I would appreciate if someone can help me to write a shell script using sed. From a larget text file I need to print a fixed value of a word. In another words whenever it finds that word, it needs to grab the other line containing "dn" and prints its value. For example:

dn: uid=poi.1111,ou=abc,o=cc.com
authpassword;orclcommonpwd: {SHA}adhnbckhsypzx=
businesscategory: abc
createtimestamp: 20050709198316x
creatorsname: cn=bulkload
modifiersname: cn=bulkload
modifytimestamp: 20050709198316x
objectclass: top
objectclass: cuser
cquestion: xyf88561

--------------------

in this example txt there are thousands of similar ones.

I need to be able to print "cquestion" and related "dn" for it using sed in a shell script. I would appreciate if someone can help me and email at "cmontr@verizon.com"

This looks more like a job for awk, but it's not entirely clear what you want.

Do you want to print any line containing "cquestion:" and the earlier line containing "dn:"?

Code:
awk '
   /^dn:/ { dn = $0 }
   /^cquestion:/ { print dn; print }' FILE

# 3  
hi

Hi again, I appreciate for looking at my question. The answer is yes. I would like to see and print cquestion, and whenever it finds cquestion then it goes back and find the dn value for that cquestion. Thank you so much if you can help.
# 4  
Quote:
Originally Posted by cfajohnson

This looks more like a job for awk, but it's not entirely clear what you want.

Do you want to print any line containing "cquestion:" and the earlier line containing "dn:"?

Code:
awk '
   /^dn:/ { dn = $0 }
   /^cquestion:/ { print dn; print }' FILE

Hi cfa - I tested that awk - works great! Thank you so much. I was wondering if there is a way you may know to use sed or perl ? I really apprecaite for the help.
# 5  
Hope this can help you!!!!

input(a):
Code:
dn: uid=poi.1111,ou=abc,o=cc.com
authpassword;orclcommonpwd: {SHA}adhnbckhsypzx=
businesscategory: abc
createtimestamp: 20050709198316x
creatorsname: cn=bulkload
modifiersname: cn=bulkload
modifytimestamp: 20050709198316x
objectclass: top
objectclass: cuser
cquestion: xyf88561
dn: uid=poi.2222,ou=def,o=dd.com
authpassword;orclcommonpwd: {SHA}adhnbckhsypzx=
businesscategory: abc
createtimestamp: 20050709198316x
creatorsname: cn=bulkload
modifiersname: cn=bulkload
modifytimestamp: 20050709198316x
objectclass: top
objectclass: cuser
cquestion: leo210375

output:
Code:
cquestion: xyf88561 ------> dn: uid=poi.1111,ou=abc,o=cc.com
cquestion: leo210375 ------> dn: uid=poi.2222,ou=def,o=dd.com

code:

Code:
awk '
{
if (index($0,"dn")!=0)
str=$0
if (index($0,"cquestion")!=0)
{
print $0,"------>",str
str=""
}
}' a

# 6  
I do appreciate for the script...If possible would you also let me know how I can pass the paremeter for cquestion ? For example there may be many different questions and I would like print for certain ones needed..because log (txt) file can be huge. I appreciate in advance. If anyone else has different methods that can help, would also be appreciated. Thanks again.
# 7  
awk help please to pass parameter

Could anyone please help me to pass a parameter 'cquestion' to extract data from a txt file from this code ? I really appreciate. I wanted to print cquestions when needed because the txt file can be over 8GB. Not want to print all. Thanks much...
----------------------------

awk '
{
if (index($0,"dn")!=0)
str=$0
if (index($0,"cquestion")!=0)
{
print $0,"------>",str
str=""
}
}' a
Login or Register for Dates, Times and to Reply

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

Test Your Knowledge in Computers #617
Difficulty: Medium
If you place two string literals side by side, they are automatically concatenated by Python.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears i have text file like this: INPUT.txt 001_1_173 j nuh ]az 001_1_174 j ]esma. nuh ]/.xori . . . and have another text like this TABLE.txt j j nuh word1... (6 Replies)
Discussion started by: alii
6 Replies

2. Shell Programming and Scripting

Converting txt file into CSV using awk or sed

Hello folks I have a txt file of information about journal articles from different fields. I need to convert this information into a format that is easier for computers to manipulate for some research that I'm doing on how articles are cited. The file has some header information and then details... (8 Replies)
Discussion started by: ksk
8 Replies

3. Shell Programming and Scripting

sed -Replacing file path within .txt file

Hi, I am trying to use sed to replace a file path within all the .lay (.txt) files in a folder. I feel that this should be easy but I can't get it to work no matter what i try. I'm using cygwin. For a .txt file containing the below line I want to replace this file path with a new one. ... (1 Reply)
Discussion started by: carlr
1 Replies

4. Shell Programming and Scripting

awk to print value from txt file to csv

Hi, I want to print two columns from a .txt file to a .csv file using awk. data in text file: Application -------------------------------------------------- ----------- OS Related Issues 1 EMEA Solutions ... (8 Replies)
Discussion started by: prashu_g
8 Replies

5. Shell Programming and Scripting

awk/sed script to print each line to a separate named file

I have a large 3479 line .csv file, the content of which looks likes this: 1;0;177;170;Guadeloupe;x 2;127;171;179;Antigua and Barbuda;x 3;170;144;2;Umpqua;x 4;170;126;162;Coos Bay;x ... 1205;46;2;244;Unmak Island;x 1206;47;2;248;Yunaska Island;x 1207;0;2;240;north sea;x... (5 Replies)
Discussion started by: kalelovil
5 Replies

6. Shell Programming and Scripting

sed to read x.txt and grep from y.txt

How would I write a command(s) to read from a file (list) that looks like this: 29847374384 and grep from a second file (list) that looks like this: 29847374384, jkdfkjdf,3833,ddd:confused: (1 Reply)
Discussion started by: smellylizzard
1 Replies

7. Shell Programming and Scripting

sed to remove last 2 characters of txt file

sed 's/^..//' file1.txt > file2.txt this will remove the first two characters of each line of a text file, what sed command will remove the last two characters? This is a similar post to my other....sry if I'm being lazy.... I need a file like this (same as last post) >cat file1.txt 10081551... (1 Reply)
Discussion started by: ajp7701
1 Replies

8. Shell Programming and Scripting

print all even lines of a txt file

In other news, I have a colors text file with hundreds of lines, and I want to print only the even numbered lines. for example I have this file looks something like this: ALLCOLORS.TXT red red green red blue red red red green red red blue green green green blue blue blue red blue blue blue... (1 Reply)
Discussion started by: ajp7701
1 Replies

9. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies

10. Shell Programming and Scripting

[Urgent]how to print the file names into a txt file???

HI, I have a folder with some 120 files...i just want to print all the file filenames(not the content or anything else) onto a file say .txt. please help me with this command Thanks a lot. (15 Replies)
Discussion started by: kumarsaravana_s
15 Replies

Featured Tech Videos