Manipulating Data

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Manipulating Data
# 1  
Old 02-05-2010
Manipulating Data

Hi. I haven't had to write bash scripts in a long time and have a simple task to do, but need some help:

Input:

chrY:22627291-22651542
chrY:23045932-23070172
chrY:23684890-23696359
chrY:25318610-25330083
chrY:25451096-25462570
chr10:1054847-1061799
chr10:1058606-1080131
chr10:1075964-1085061

Desired Output:

chrY 22627291 22651542
chrY 23045932 23070172
chrY 23684890 23696359
chrY 25318610 25330083
chrY 25451096 25462570
chr10 1054847 1061799
chr10 1058606 1080131
chr10 1075964 1085061



Also, the input is in a input.txt file, the output file should also be a text file. So basically the colon and the dash need to be replaced by one space. I'm thinking I would use the "cut c" function but not sure because the number of characters that have to be separated from the second column varies.. Any help would be great and really appreciated. Thanks!
# 2  
Old 02-05-2010
Hi.

The sed utility is one way:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate substitution and comparison.

# Infrastructure details, environment, commands for forum posts. 
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p sed cmp
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Expected output:"
cat expected-output.txt

echo
echo " Results:"
sed "s/[-:]/ /g" $FILE |
tee t1

echo 
echo " Comparison:"
cmp t1 expected-output.txt && echo OK || echo KO

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
GNU sed version 4.1.5
cmp (GNU diffutils) 2.8.1

 Data file data1:
chrY:22627291-22651542
chrY:23045932-23070172
chrY:23684890-23696359
chrY:25318610-25330083
chrY:25451096-25462570
chr10:1054847-1061799
chr10:1058606-1080131
chr10:1075964-1085061

 Expected output:
chrY 22627291 22651542
chrY 23045932 23070172
chrY 23684890 23696359
chrY 25318610 25330083
chrY 25451096 25462570
chr10 1054847 1061799
chr10 1058606 1080131
chr10 1075964 1085061

 Results:
chrY 22627291 22651542
chrY 23045932 23070172
chrY 23684890 23696359
chrY 25318610 25330083
chrY 25451096 25462570
chr10 1054847 1061799
chr10 1058606 1080131
chr10 1075964 1085061

 Comparison:
OK

cheers, drl
# 3  
Old 02-05-2010
Tr is another

Code:
tr ':-' '  '

# 4  
Old 02-06-2010
use perl code below:-

Code:
perl -wnl -e 's/:/ / and s/-/ / and print ; ' input.txt > output.txt


SmilieSmilieSmilie
# 5  
Old 02-06-2010
Code:
awk -F[:-] ' { print $1,$2,$3 } ' file

Code:
awk ' gsub("[:-]"," ") ' file

Code:
sed "y/:-/  /" file

# 6  
Old 02-06-2010
more shorter code:-

Code:
perl -wpl -e 's/(-|:)/ /g ;'  input.txt

SmilieSmilieSmilie
# 7  
Old 02-06-2010
Code:
awk -F[:-] '$1=$1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Manipulating Data Records for reporting

Hello All, I have Data Records (DRs) with the following format: ... (2 Replies)
Discussion started by: EAGL€
2 Replies

2. Shell Programming and Scripting

Data manipulating script. Please HELP!

Dear friends, I'm struggling to preparing a bunch of gromacs input files, say manually. It's really a time-consuming work without any techniques. I suppose that it could be done by a smart script automatically. But I lack some basic knowledge on scripting. Please help! My original input looks... (3 Replies)
Discussion started by: liuzhencc
3 Replies

3. Shell Programming and Scripting

Manipulating xml data with awk

Hi everyone, I have a little bit of complicated task to finish with AWK. Here it is; I have a data file in xml format which looks like this <data> a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 c1 c2 c3 c4 c5 d1 d2 d3 d4 d5 e1 e2 e3 e4 e5 </data> lets say each data block contains 5 rows and 5 columns,... (13 Replies)
Discussion started by: hayreter
13 Replies

4. Shell Programming and Scripting

Manipulating Filenames

Hi Folks, I'm looking for some ideas on how to change some file names. I'm pretty sure I need to use sed or awk but they still escape me. The files I have are like: VOD0615 NEW Blades R77307.pdf or VOD0615_NEW_Blades_R77307.pdf and what I want after processing is: R77307 NEW Blades.pdf ... (5 Replies)
Discussion started by: imonkey
5 Replies

5. Shell Programming and Scripting

reading from two files and manipulating the data

hi i have a file of the following format FILE1 5 937 8 1860 1850 1 683 2 1 129 2 2 5 938 8 1122 1123 1 20 520 4 1860 1851 1 5 939 8 1122 1124 1 20 521 4i have another file which... (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

6. Shell Programming and Scripting

Manipulating a file

Hi everybody, I need an urgent help with a BASH script. I have file which contains (besides the other data) the lines with the following structure identified by with keyword PCList: <PARAMETER NAME="PCList" TYPE="LIST_STRUCTURE" MODEL="{,}" ... (1 Reply)
Discussion started by: sameucho
1 Replies

7. Shell Programming and Scripting

manipulating data

Hi guys Firstly, I'd like to say hi and how great this forum is. I'm not new to UNIX but am relatively new to scripting. I have a personal project that I'm working on just to try and speed up my learning. I working with a text file, well more of a logfile really. It has several columns of... (6 Replies)
Discussion started by: abcd69
6 Replies

8. Shell Programming and Scripting

Manipulating Pick multi dimensional data with awk.

Hi. I am reasonably new to awk, but have done quite a lot of unix scripting in the past. I have resolved the issues below with unix scripting but it runs like a dog. Moved to awk for speed and functionality but running up a big learning curve in a hurry, so hope there is some help here. I... (6 Replies)
Discussion started by: mike.strategis
6 Replies

9. UNIX for Dummies Questions & Answers

Help!! manipulating file

Hi all, I need help manipulating the file below. Here is what I needed to do. First, I have to replace INSUPD to DELETE. Then I need to change the content of the file around by flipping the contents in the file from the bottom to the top (start from "CMD") How should I attack this? Here... (2 Replies)
Discussion started by: sirrtuan
2 Replies

10. Shell Programming and Scripting

Manipulating data in variable

Hi, I have two variables - A and B - containing a bunch of file paths. I am comparing them and when I find a match I want to remove that entry from A so that as the compare proceeds A shrinks entry by entry. How can I remove a matched entry from A whilst leaving the non matched entries... (6 Replies)
Discussion started by: ajcannon
6 Replies
Login or Register to Ask a Question