Sponsored Content
Top Forums Shell Programming and Scripting "How to get an exact string from a txt file?" Post 302363052 by durden_tyler on Monday 19th of October 2009 09:42:48 AM
Old 10-19-2009
Quote:
Originally Posted by liuzhencc
...
I'm trying to get this exact string from these txt files.

example 1,
2.524075,-0.563322,-1.285286\H,0,-2.544438,-0.678834,1.199166\H,0,2.18
5962,-1.978001,0.018499\\Version=EM64T-G03RevE.01\State=5-A\HF=-1277.1
557592
\S2=6.033269\S2-1=0.\S2A=6.000179\RMSD=8.037e-05\Thermal=0.\Dipo
le=-0.1643425,0.9094768,0.0321427\PG=C01 [X(C4H9Cr1O1)]\\@

example 2,
6256\H,0,-2.980236,0.00009,-0.45647\\Version=EM64T-G03RevE.01\State=5-
A\HF=-1198.5241253\S2=6.077753\S2-1=0.\S2A=6.000457\RMSD=4.977e-05\The
rmal=0.\Dipole=0.8534315,0.002042,-0.5745813\PG=C01 [X(C2H5Cr1O1)]\\@

...
If "example 1" and "example 2" are examples of a single lines (i.e. they do not span multiple lines in your text file), then here's a way to do it with Perl:

Code:
$
$ cat -n f1
     1  2.524075,-0.563322,-1.285286\H,0,-2.544438,-0.678834,1.199166\H,0,2.185962,-1.978001,0.018499\\Version=EM64T-G03RevE.01\State=5-A\HF=-1277.1557592\S2=6.033269\S2-1=0.\S2A=6.000179\RMSD=8.037e-05\Thermal=0.\Dipole=-0.1643425,0.9094768,0.0321427\PG=C01 [X(C4H9Cr1O1)]\\@
     2  6256\H,0,-2.980236,0.00009,-0.45647\\Version=EM64T-G03RevE.01\State=5-A\HF=-1198.5241253\S2=6.077753\S2-1=0.\S2A=6.000457\RMSD=4.977e-05\Thermal=0.\Dipole=0.8534315,0.002042,-0.5745813\PG=C01 [X(C2H5Cr1O1)]\\@
$
$ perl -lne '/.*(HF=[^\\]+)\\.*/ and print $1' f1
HF=-1277.1557592
HF=-1198.5241253
$
$

You can redirect the output to a new file using the shell's redirection operator.

tyler_durden

##
Sorry, did not read your post carefully.
For the ones that span multiple lines, here's how you can do it with Perl:

Code:
$
$ cat -n f2
     1  example 1,
     2  2.524075,-0.563322,-1.285286\H,0,-2.544438,-0.678834,1.199166\H,0,2.18
     3  5962,-1.978001,0.018499\\Version=EM64T-G03RevE.01\State=5-A\HF=-1277.1
     4  557592\S2=6.033269\S2-1=0.\S2A=6.000179\RMSD=8.037e-05\Thermal=0.\Dipo
     5  le=-0.1643425,0.9094768,0.0321427\PG=C01 [X(C4H9Cr1O1)]\\@
     6  example 2,
     7  6256\H,0,-2.980236,0.00009,-0.45647\\Version=EM64T-G03RevE.01\State=5-
     8  A\HF=-1198.5241253\S2=6.077753\S2-1=0.\S2A=6.000457\RMSD=4.977e-05\The
     9  rmal=0.\Dipole=0.8534315,0.002042,-0.5745813\PG=C01 [X(C2H5Cr1O1)]\\@
$
$ perl -lne 'BEGIN {undef $/} while(/.*(HF=[^\\]+)\\.*/mg){$x=$1; $x=~s/\n//g; print $x}' f2
HF=-1277.1557592
HF=-1198.5241253
$

tyler_durden
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

2. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

3. Shell Programming and Scripting

grep regex, match exact string which includes "/" anywhere on line.

I have a file that contains the 2 following lines (from /proc/mounts) /dev/sdc1 /mnt/backup2 xfs rw,relatime,attr2,noquota 0 0 /dev/sdb1 /mnt/backup xfs rw,relatime,attr2,noquota 0 0 I need to match the string in the second column exactly so that only one result is returned, e.g. > grep... (2 Replies)
Discussion started by: jelloir
2 Replies

4. Shell Programming and Scripting

Compare two string and get "exact" difference only

Hi all; Pretty green to perl programming; been searching high and low for a perl (preferably) or unix script that will compare 2 CSV strings in the same file that are separated buy the "|" character (so basically they're side by side) and give the results of ONLY the exact change; note that 19... (3 Replies)
Discussion started by: gvolpini
3 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

7. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

8. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

9. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

10. Programming

[Python] replicating "sha256 -C checksum_file.txt file.txt"

Hello everyone, Since my python knowledge is limimted, I've challenged myself to learn as much as possible to help me with my carrere. I'm currently trying to convert a shell script to python, just to give myself a task. There is one section of the script that I'm having issues converting and... (2 Replies)
Discussion started by: da1
2 Replies
dos2unix(1)						      General Commands Manual						       dos2unix(1)

NAME
dos2unix - DOS/MAC to UNIX text file format converter SYNOPSYS
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...] Options: [-hkqV] [--help] [--keepdate] [--quiet] [--version] DESCRIPTION
This manual page documents dos2unix, the program that converts plain text files in DOS/MAC format to UNIX format. OPTIONS
The following options are available: -h --help Print online help. -k --keepdate Keep the date stamp of output file same as input file. -q --quiet Quiet mode. Suppress all warning and messages. -V --version Prints version information. -c --convmode convmode Sets conversion mode. Simulates dos2unix under SunOS. -o --oldfile file ... Old file mode. Convert the file and write output to it. The program default to run in this mode. Wildcard names may be used. -n --newfile infile outfile ... New file mode. Convert the infile and write output to outfile. File names must be given in pairs and wildcard names should NOT be used or you WILL lost your files. EXAMPLES
Get input from stdin and write output to stdout. dos2unix Convert and replace a.txt. Convert and replace b.txt. dos2unix a.txt b.txt dos2unix -o a.txt b.txt Convert and replace a.txt in ASCII conversion mode. Convert and replace b.txt in ISO conversion mode. Convert c.txt from Mac to Unix ascii format. dos2unix a.txt -c iso b.txt dos2unix -c ascii a.txt -c iso b.txt dos2unix -c mac a.txt b.txt Convert and replace a.txt while keeping original date stamp. dos2unix -k a.txt dos2unix -k -o a.txt Convert a.txt and write to e.txt. dos2unix -n a.txt e.txt Convert a.txt and write to e.txt, keep date stamp of e.txt same as a.txt. dos2unix -k -n a.txt e.txt Convert and replace a.txt. Convert b.txt and write to e.txt. dos2unix a.txt -n b.txt e.txt dos2unix -o a.txt -n b.txt e.txt Convert c.txt and write to e.txt. Convert and replace a.txt. Convert and replace b.txt. Convert d.txt and write to f.txt. dos2unix -n c.txt e.txt -o a.txt b.txt -n d.txt f.txt DIAGNOSTICS
BUGS
The program does not work properly under MSDOS in stdio processing mode. If you know why is that so, please tell me. AUTHORS
Benjamin Lin - <blin@socs.uts.edu.au> Bernd Johannes Wuebben (mac2unix mode) <wuebben@kde.org> MISCELLANY
Tested environment: Linux 1.2.0 with GNU C 2.5.8 SunOS 4.1.3 with GNU C 2.6.3 MS-DOS 6.20 with Borland C++ 4.02 Suggestions and bug reports are welcome. SEE ALSO
unix2dos(1) mac2unix(1) 1995.03.31 dos2unix v3.0 dos2unix(1)
All times are GMT -4. The time now is 03:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy