Sponsored Content
Top Forums Shell Programming and Scripting Assigning a specific format to a specific column in a text file using awk and printf Post 302417447 by goodbenito on Thursday 29th of April 2010 03:56:46 PM
Old 04-29-2010
Assigning a specific format to a specific column in a text file using awk and printf

Hi,

I have the following text file:

8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm
9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm
10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm
11 T1mapping_flip15 ok 128 108 30 1 665000-000011-000001.dcm
12 T1mapping_flip30 ok 128 108 30 1 665000-000012-000001.dcm
15 T1mapping_flip02 ok 128 108 30 1 665000-000015-000001.dcm
16 T1mapping_flip05 ok 128 108 30 1 665000-000016-000001.dcm
17 T1mapping_flip10 ok 128 108 30 1 665000-000017-000001.dcm
18 T1mapping_flip15 ok 128 108 30 1 665000-000018-000001.dcm
19 T1mapping_flip30 ok 128 108 30 1 665000-000019-000001.dcm

Using the following code, I've managed to get this far:
Code:
while read line ; 
do echo $line | awk '{print $2 "/" $1 "/" $8}' ; 
done < file

...to produce this output:

T1mapping_flip02/8/665000-000008-000001.dcm
T1mapping_flip05/9/665000-000009-000001.dcm
T1mapping_flip10/10/665000-000010-000001.dcm
T1mapping_flip15/11/665000-000011-000001.dcm
T1mapping_flip30/12/665000-000012-000001.dcm
T1mapping_flip02/15/665000-000015-000001.dcm
T1mapping_flip05/16/665000-000016-000001.dcm
T1mapping_flip10/17/665000-000017-000001.dcm
T1mapping_flip15/18/665000-000018-000001.dcm
T1mapping_flip30/19/665000-000019-000001.dcm

The problem is, I would like the middle number (field #3) to be a 3-digit number, e.g. 008, 009, 010...019.

I've tried this:

Code:
while read line ; 
do echo $line | awk '{print $2 "/" {printf "%03d\n" $1} "/" $8}' ; 
done < file

...but obviously I don't know what I'm doing because I get a big ugly syntax error.

Any help would be greatly appreciated as always.
Thanks!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

2. UNIX for Dummies Questions & Answers

AWK Command to find text in specific column

I'm new to scripting and would appreciate any help. I have a list of over 20 words in File1 that I need to find in columns 10-15 of File2. I need the entire row of File2 that the File1 list matches. I originally used a grep command which works, but provides File1 results that can be found... (3 Replies)
Discussion started by: Chillin
3 Replies

3. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

4. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

5. UNIX for Dummies Questions & Answers

Use sed to replace but only in a specific column of the text file

Hi, I would like to use sed to replace NA to x ('s/NA/x/g'), but only in the 5th column of the space delimited text file, nowhere else. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. UNIX for Dummies Questions & Answers

Switching the values in a specific column of a text file

Hi, I have a space de-limited text file. In the fifth column, I would like to switch "1"s with "2"s. How do I go about doing that? Thanks! Sample input: 0 311000259 0 0 1 1 0 311000397 0 0 1 2 0 311000491 0 0 2 1 0 311000516 0 0 2 1 0 311000541 0 0 1 1 0 311000558 0 0 2 1 0 311000566 0... (1 Reply)
Discussion started by: evelibertine
1 Replies

7. UNIX for Dummies Questions & Answers

How to cut from a text file based on value of a specific column?

Hi, I have a tab delimited text file from which I want to cut out specific columns. If the second column equals one, I want to cut out columns 1 and 5 and 6. If the second column equals two, I want to cut out columns 1 and 5 and 7. How do I go about doing that? Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

8. Shell Programming and Scripting

Converting text files to xls through awk script for specific data format

Dear Friends, I am in urgent need for awk/sed/sh script for converting a specific data format (.txt) to .xls. The input is as follows: >gi|1234|ref| Query = 1 - 65, Target = 1677 - 1733 Score = 8.38, E = 0.6529, P = 0.0001513, GC = 46 fd sdfsdfsdfsdf fsdfdsfdfdfdfdfdf... (6 Replies)
Discussion started by: Amit1
6 Replies

9. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

10. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies
gdcmconv(1)							       GDCM							       gdcmconv(1)

NAME
gdcmconv - Tool to convert DICOM to DICOM. SYNOPSIS
gdcmconv [options] file-in file-out DESCRIPTION
The gdcmconv command line program takes as input a DICOM file (file-in) and process it to generate an output DICOM file (file-out). The command line option dictate the type of operation(s) gdcmconv will use to generate the output file. PARAMETERS
file-in DICOM input filename file-out DICOM output filename options PARAMETERS -i --input DICOM filename -o --output DICOM filename options -X --explicit Change Transfer Syntax to explicit. -M --implicit Change Transfer Syntax to implicit. -U --use-dict Use dict for VR (only public by default). --with-private-dict Use private dict for VR (advanced user only). -C --check-meta Check File Meta Information (advanced user only). --root-uid Root UID. --remove-gl Remove group length (deprecated in DICOM 2008). --remove-private-tags Remove private tags. --remove-retired Remove retired tags. image options -l --apply-lut Apply LUT (non-standard, advanced user only). -P --photometric-interpretation %s Change Photometric Interpretation (when possible). -w --raw Decompress image. -d --deflated Compress using deflated (gzip). -J --jpeg Compress image in jpeg. -K --j2k Compress image in j2k. -L --jpegls Compress image in jpeg-ls. -R --rle Compress image in rle (lossless only). -F --force Force decompression/merging before recompression/splitting. --compress-icon Decide whether icon follows main TransferSyntax or remains uncompressed. --planar-configuration [01] Change planar configuration. -Y --lossy Use the lossy (if possible) compressor. -S --split %d Write 2D image with multiple fragments (using max size) JPEG options -q --quality %*f set quality. JPEG-LS options -e --lossy-error %*i set error. J2K options -r --rate %*f set rate. -q --quality %*f set quality. -t --tile %d,%d set tile size. -n --number-resolution %d set number of resolution. --irreversible set irreversible. general options -h --help print this help text and exit -v --version print version information and exit -V --verbose verbose mode (warning+error). -W --warning warning mode, print warning information -E --error error mode, print error information -D --debug debug mode, print debug information special options -I --ignore-errors convert even if file is corrupted (advanced users only, see disclaimers). environment variable GDCM_ROOT_UID Root UID Simple usage gdcmconv is a great tool to convert broken DICOM implementation into properly parsable DICOM file. Usage is simply: $ gdcmconv input.dcm output.dcm or if you prefer being explicit: $ gdcmconv -i input.dcm -o output.dcm Even though gdcmconv can overwrite directly on the same file (input.dcm = output.dcm), it is recommended that user should first convert into a different file to make sure the bug is properly handled by GDCM. Typical cases where you would want to use gdcmconv in its simple form: o convert non-cp246 conforming file into conforming cp246, o convert implicit little endian transfer syntax file meta header into proper explicit little endian transfer syntax, o convert the GE-13 bytes bug, o convert dual syntax file: implicit/explicit, o convert Philips dual Little Endian/Big Endian file, o convert GDCM 1.2.0 broken UN-2-bytes fields, o &... o All other broken files listed in the supported section. When no option other is used, only the dataset is inspected. So encapsulated Pixel Data, for instance, is not inspected for well known bugs. When doing this kind of work, this is usually a good idea to perform some kind of quality control, see gdcmconv Quality Control section (down below). Typical usage File Meta Header Running $ gdcmconv input.dcm output.dcm Is not enough to recompute file meta header, when input file is buggy. You may want to use: --check-meta $ gdcmconv --check-meta input.dcm output.dcm See typical cases such as: GE_DLX-8-MONO2-PrivateSyntax.dcm or PICKER-16-MONO2-No_DicomV3_Preamble.dcm from gdcmData. Conversion to Explicit Transfer Syntax To convert a file that was written using Implicit Transfer Syntax into Explicit Transfer Syntax simply use: $ gdcmconv --explicit uncompressed.dcm compressed.dcm Compressing to lossless JPEG To compress an uncompressed DICOM file to a JPEG Lossless encapsulated format: $ gdcmconv --jpeg uncompressed.dcm compressed.dcm Compressing to lossy JPEG To compress an uncompressed DICOM file to a JPEG Lossy encapsulated format: $ gdcmconv --lossy --jpeg -q 90 uncompressed.dcm compressed.dcm Note: -q is just one of the many way to specify lossy quality, you need to inspect the other cmd line flag to specify lossyness properties. Compressing to lossless JPEG-LS To compress an uncompressed DICOM file to a JPEG-LS Lossless encapsulated format: $ gdcmconv --jpegls uncompressed.dcm compressed.dcm Compressing to lossy JPEG-LS To compress an uncompressed DICOM file to a JPEG-LS Lossy encapsulated format: $ gdcmconv --lossy --jpegls -e 2 uncompressed.dcm lossy_compressed.dcm Note: -e (or --lossy-error) means that the maximum tolerate error is 2 for each pixel value Compressing to lossless J2K To compress an uncompressed DICOM file to a JPEG-2000 Lossless encapsulated format: $ gdcmconv --j2k uncompressed.dcm compressed.dcm Compressing to lossy J2K To compress an uncompressed DICOM file to a JPEG-2000 Lossy encapsulated format: $ gdcmconv --lossy -q 55,50,45 --j2k uncompressed.dcm lossy_compressed.dcm Note: -q is just one of the many way to specify lossy quality, you need to inspect the other cmd line flag to specify lossyness properties. Compressing to lossless RLE To compress an uncompressed DICOM file to a RLE Lossless encapsulated format: $ gdcmconv --rle uncompressed.dcm compressed.dcm There is no such thing as lossy RLE compression. Forcing (re)compression Sometime it is necessary to use the --force option. By default when user specify --j2k and input file is already in JPEG 2000 encapsulated DICOM format then no operation takes places. By using --force you make sure that (re)compression operation takes places. Real life example of why you would use --force: o When Pixel Data is missing data / is padded with junk o When you would like to make sure GDCM can handle decompression & recompression cycle Decompressing a Compressed DICOM $ gdcmconv --raw compressed.dcm uncompressed.dcm Compressing an uncompressed Icon By default when compressing a DICOM Image file, gdcmconv will not compress the icon. A user option needs to be turned on to explicitely force the compression of the Icon Image Sequence Pixel Data For example, by default we will not compress the Icon Image Sequence Pixel Data attribute: $ gdcmconv --jpeg gdcmData/simpleImageWithIcon.dcm uncompressed_icon.dcm In the following example we will explicitely compress the Icon Image Sequence Pixel Data attibute. In that case the same Transfer Syntax is being used for both the main Pixel Data and the Pixel Data from the Icon Image Sequence: $ gdcmconv --jpeg --compress-icon gdcmData/simpleImageWithIcon.dcm compressed_icon.dcm Generating an Icon For some application it might be necessary to produce a small preview of the main image to be able to quickly load that short preview instead of the main image. In that case: gdcmconv --raw --generate-icon gdcmData/test.acr test_icon.dcm In some cases the main Pixel Data element is expressed as pixel defined on 16bits. Since Icon can only store at most pixel of size 8bits, a rescale operation needs to take place. In order to properly select a better interval for doing the rescale operation user can specify the min max used for the rescale operation: gdcmconv --raw --generate-icon --icon-minmax 0,192 gdcmData/012345.002.050.dcm icon_minmax.dcm Changing the planar Configuration Often RLE files are compressed using a different Planar Comnfiguration (RRR ... GGG... BBB...) instead of the usual triplet (RGB ... RGB ... RGB ). So upon decompression the Planar Configuration is 1. This is perfectly legal in DICOM, however this is unconventional, and thus it may be a good idea to also change the planar configuration and set it to the default : $ gdcmconv --raw --planar-configuration 0 compressed.dcm uncompressed1.dcm To reinvert the planar configuration of file 'uncompressed1.dcm', simply do: $ gdcmconv --raw --planar-configuration 1 uncompressed1.dcm uncompressed2.dcm Lossless Conversion When talking about lossless conversion, there is an ambiguity that need to be understood. To achieve higher compression ratio, the RGB color space is usually not used, in favor of a YBR one. Changing from one color space to the other is (bit level) not lossless. For more detail, see what are the true lossless transformations as described: http://sourceforge.net/apps/mediawiki/gdcm/index.php?title=Color_Space_Transformations Quality Control One important part when using gdcmconv it to have a way to quality control the output. You can use 3rd party tool to check the output of gdcmconv is correct. DCMTK / dicom3tools Using another DICOM implementation such as the one from DCMTK or dicom3tools can be a good process to check the output of gdcmconv. o For DCMTK use: dcmdump o For dicom3tools use: dcdump For reference, gdcmconv --raw will act as dcmdjpeg +cn +px, since it never tries to convert color space. VIM: vimdiff You can setup your favorite editor to compare the output, for instance in vim: autocmd BufReadPre *.dcm set ro autocmd BufReadPost *.dcm silent %!dcmdump -M +uc '%' then simply do: $ vimdiff input.dcm output.dcm vbindiff On UNIX you can visually compare binary file using the vbindiff command: $ vbindiff input.dcm output.dcm SEE ALSO
gdcmdump(1), gdcmraw(1), gdcminfo(1) COPYRIGHT
Copyright (c) 2006-2011 Mathieu Malaterre Version 2.2.0 Tue Feb 5 2013 gdcmconv(1)
All times are GMT -4. The time now is 12:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy