Sponsored Content
Top Forums Shell Programming and Scripting Formatting file data to another file (control character related) Post 302704595 by bakunin on Saturday 22nd of September 2012 11:41:24 AM
Old 09-22-2012
I suppose the problem being the "echo" statement. It is fed the content of the variable "$line" and interprets it. "\n" and "\t" in your example are control characters, though. "\n" is "newline", "\t" is tab.

Anyhow, your implementation isn't all too good anyway, because you use several commands and a pipeline to do what shell exansion could do too - at considerably less computing costs:

Code:
x="abc|def|ghi|jkl"
echo "${x%%|*}"
echo "${x#*|}"
y="${x#*|}"
x="${x#${y}|}"
echo "${x%%|*}"
echo "${x#*|}"

Change your code accordingly and it should not only work but run faster too.

An independent observation: DO NOT use backticks any more. They are deprecated and the shell only understands them for backward compatibility issues. Use the modern "$(...)" instead, which is a lot more flexible, can be nested, can be quoted, ...

Another hint: in case you write for Korn shell you might consider using file descriptors instead of redirection. Instead of:

Code:
command1 > "$file"
command2 >> "$file"
command3 >> "$file"
...etc.

you can write:

Code:
exec 3> "$file"     # file descriptor 3 to write into $file
# exec 3>> "$file"    # alternatively open it in append mode

print -u3 - "first line"        # -u3 is FD 3
print -u3 - "second line"
print -u3 - "third line"

exec 3>&-                 # close FD 3

I hope this helps.

bakunin

Last edited by bakunin; 09-22-2012 at 12:50 PM..
This User Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Control character in a file

Hi All, I am looking for a solution to capture any ASCII control character in a file ( where the ASCII control character is in decimal value from 0 to 31 and 127 ( Hex value from 00 to 1F and 7F ) ) by returning any affected lines. The intended good file should contain "ASCII printable... (5 Replies)
Discussion started by: cursive
5 Replies

2. Shell Programming and Scripting

Checking for a control file before processing a data file

Hi All, I am very new to Shell scripting... I got a requirement. I will have few text files(data files) in a particular directory. they will be with .txt extension. With same name, but with a different extension control files also will be there. For example, Sample_20081001.txt is the data... (4 Replies)
Discussion started by: purna.cherukuri
4 Replies

3. UNIX for Dummies Questions & Answers

How to find the ^M(control M) character in unix file?

can any one say about command to find "^M" (Control M)characters in a unix text file. ^M comes when a file ftped from windows to unix without using bin mode. I need the command to find lik this, ex.txt: ------------------------------ ...,name,time^M go^M ...file,end^M... (5 Replies)
Discussion started by: prsam
5 Replies

4. Shell Programming and Scripting

formatting data file with awk or sed

Hi, I have a (quite large) data file which looks like: _____________ header part.. more header part.. x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 ... ... x59 x60 y1 y2 y3 y4... ... y100 ______________ where x1, x2,...,x60 and y1, y2,...y100 are numbers of 10 digits (so each line... (5 Replies)
Discussion started by: lego
5 Replies

5. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

6. Programming

Problem with control file and special character

I am getting error when loading data file using ctl file. I get this error only when there is special character. Below is some data. DataFile=> company_id|ciu_id|english_name|iso_country_code|active|partner_name 1-2JT-122||Expert Järvenpää|FI|A|Expert Järvenpää Control File=> LOAD DATA... (1 Reply)
Discussion started by: rshivarkar
1 Replies

7. Shell Programming and Scripting

perl cmd to remove the control-Z character at end of 10GB file

In a 10-50GB file , at end of file there is Control-z character tried the below options, 1. perl -p -i -e 's/^Z//g' new.txt 2. perl -0777lwi -032e0 new.txt and Sed command, dos2unix etc it takes more time to remove the control-z. need a command or perl program to GO TO LAST LINE OF FILE ... (7 Replies)
Discussion started by: prsam
7 Replies

8. Shell Programming and Scripting

control M character in unix file

in a file we are getting control character in a file , is there any way that they can be removed once we have the file for eg. BEGIN-PROCEDURE INITIALIZE ^M LET #row_count = 0^M ^M ^M (2 Replies)
Discussion started by: lalitpct
2 Replies

9. UNIX for Dummies Questions & Answers

Formatting data in a raw file by using another mapping file

Hi All, i have a requirement where i need to format the input RAW file ( which is CSV) by using another mapping file(also CSV file). basically i am getting feed file with dynamic headers by using mapping file (in that target field is mapped with source filed) i have to convert the raw file into... (6 Replies)
Discussion started by: ravi4informatic
6 Replies

10. Shell Programming and Scripting

Formatting data to put it in the excel file

Hello, I have a file with the below contents : Policy Name: Backup_bkp Policy Type: Catalog_bkp Active: yes Effective date: 08/07/2013 02:02:12 Mult. Data Streams: no Client Encrypt: no Checkpoint: no Policy Priority: ... (11 Replies)
Discussion started by: rahul2662
11 Replies
dpkg-query(1)							    dpkg suite							     dpkg-query(1)

NAME
dpkg-query - a tool to query the dpkg database SYNOPSIS
dpkg-query [option...] command DESCRIPTION
dpkg-query is a tool to show information about packages listed in the dpkg database. COMMANDS
-l, --list [package-name-pattern...] List packages matching given pattern. If no package-name-pattern is given, list all packages in /var/lib/dpkg/status, excluding the ones marked as not-installed (i.e. those which have been previously purged). Normal shell wildchars are allowed in package-name-pat- tern. Please note you will probably have to quote package-name-pattern to prevent the shell from performing filename expansion. For example this will list all package names starting with "libc6": dpkg-query -l 'libc6*' The first three columns of the output show the desired action, the package status, and errors, in that order. Desired action: u = Unknown i = Install h = Hold r = Remove p = Purge Package status: n = Not-installed c = Config-files H = Half-installed U = Unpacked F = Half-configured W = Triggers-awaiting t = Triggers-pending i = Installed Error flags: <empty> = (none) R = Reinst-required An uppercase status or error letter indicates the package is likely to cause severe problems. Please refer to dpkg(1) for informa- tion about the above states and flags. The output format of this option is not configurable, but varies automatically to fit the terminal width. It is intended for human readers, and is not easily machine-readable. See -W (--show) and --showformat for a way to configure the output format. -W, --show [package-name-pattern...] Just like the --list option this will list all packages matching the given pattern. However the output can be customized using the --showformat option. The default output format gives one line per matching package, each line having the name (extended with the architecture qualifier for Multi-Arch same packages) and installed version of the package, separated by a tab. -s, --status package-name... Report status of specified package. This just displays the entry in the installed package status database. When multiple package- name are listed, the requested status entries are separated by an empty line, with the same order as specified on the argument list. -L, --listfiles package-name... List files installed to your system from package-name. When multiple package-name are listed, the requested lists of files are sepa- rated by an empty line, with the same order as specified on the argument list. However, note that files created by package-specific installation-scripts are not listed. --control-list package-name List control files installed to your system from package-name. These can be used as input arguments to --control-show. --control-show package-name control-file Print the control-file installed to your system from package-name to the standard output. -c, --control-path package-name [control-file] List paths for control files installed to your system from package-name. If control-file is specified then only list the path for that control file if it is present. Warning: this command is deprecated, please switch to use --control-list and --control-show instead. -S, --search filename-search-pattern... Search for packages that own files corresponding to the given pattern. Standard shell wildchars can be used in the pattern. This command will not list extra files created by maintainer scripts, nor will it list alternatives. -p, --print-avail package-name... Display details about package-name, as found in /var/lib/dpkg/available. When multiple package-name are listed, the requested avail- able entries are separated by an empty line, with the same order as specified on the argument list. Users of APT-based frontends should use apt-cache show package-name instead as the available file is only kept up-to-date when using dselect. -?, --help Show the usage message and exit. --version Show the version and exit. OPTIONS
--admindir=dir Change the location of the dpkg database. The default location is /var/lib/dpkg. --load-avail Also load the available file when using the --show and --list commands, which now default to only querying the status file. -f, --showformat=format This option is used to specify the format of the output --show will produce. The format is a string that will be output for each package listed. In the format string, "" introduces escapes: newline carriage return tab "" before any other character suppresses any special meaning of the following character, which is useful for "" and "$". Package information can be included by inserting variable references to package fields using the syntax "${field[;width]}". Fields are printed right-aligned unless the width is negative in which case left alignment will be used. The following fields are recog- nized but they are not necessarily available in the status file (only internal fields or fields stored in the binary package end up in it): Architecture Bugs Conffiles (internal) Config-Version (internal) Conflicts Breaks Depends Description Enhances Essential Filename (internal, front-end related) Homepage Installed-Size MD5sum (internal, front-end related) MSDOS-Filename (internal, front-end related) Maintainer Origin Package Pre-Depends Priority Provides Recommends Replaces Revision (obsolete) Section Size (internal, front-end related) Source Status (internal) Suggests Tag (usually not in .deb but in repository Packages files) Triggers-Awaited (internal) Triggers-Pending (internal) Version The following are virtual fields, generated by dpkg-query from values from other fields (note that these do not use valid names for fields in control files): binary:Package binary:Summary db:Status-Abbrev source:Package source:Version The default format string is "${binary:Package} ${Version} ". Actually, all other fields found in the status file (i.e. user defined fields) can be requested, too. They will be printed as-is, though, no conversion nor error checking is done on them. binary:Package is a special field that will print the package name with an architecture qualifier (like "libc6:amd64") if the pack- age has a Multi-Arch field with a value of same, and as such its name could be ambiguous. To get the name of the dpkg maintainer and the installed version, you could run: dpkg-query -W -f='${binary:Package} ${Version} ${Maintainer} ' dpkg EXIT STATUS
0 The requested query was successfully performed. 1 Problems were encountered while parsing the command line or performing the query, including no file or package being found (except for --control-path). ENVIRONMENT
DPKG_ADMINDIR If set and the --admindir option has not been specified, it will be used as the dpkg data directory. COLUMNS This setting influences the output of the --list option by changing the width of its output. SEE ALSO
dpkg(1). Debian Project 2012-04-03 dpkg-query(1)
All times are GMT -4. The time now is 07:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy