How to make the cut command read a column after logical statements are meet?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make the cut command read a column after logical statements are meet?
# 1  
Old 02-05-2013
How to make the cut command read a column after logical statements are meet?

Hi,

I want to write a code that will cut everything that is under a couple of columns in a text file but so far I am not even close to achieve something like that.

Basically, I want to cut all the information from the columns and transfer the column information to a spread sheet.

I have the solution in my mind, but in computer logic it is harder.

The solution is:

Code:
If (delimiter = ITEM) then cut 1-6
if (delimiter = DESCRIPTION) then cut 8-37
if (delimiter = PRICE) then cut 39-47

and so on...

If you think there is an easier way to transfer the information in the columns to a spread sheet please let me know.

Thank you!

Last edited by jim mcnamara; 02-06-2013 at 12:01 AM..
# 2  
Old 02-05-2013
Here is an example of using -i -d or -p script args to control what is cut. Example usage would be get_field -i item.csv

Code:
#!/bin/bash
while getopts idp opt 2> /dev/null
do
    case $opt in
        i) CUT_ARGS=" -c 1-6" ;;
        d) CUT_ARGS=" -c 8-37" ;;
        p) CUT_ARGS=" -c 39-47" ;;
        *) echo "Illegal option" >&2
           exit 2
        ;;
    esac
done
shift $((OPTIND-1))

if [ -z "$CUT_ARGS" ]
then
    echo "Must specify one of -i -d or -p" >&2
    exit 3
fi

if [ $# -ne 1 ]
then
    echo "Must specify output file" >&2
    exit 4
fi

cut $CUT_ARGS new.txt > $1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

2. Shell Programming and Scripting

Read first column and count lines in second column using awk

Hello all, I would like to ask your help here: I've a huge file that has 2 columns. A part of it is: sorted.txt: kss23 rml.67lkj kss23 zhh.6gf kss23 nhd.09.fdd kss23 hp.767.88.89 fl67 nmdsfs.56.df.67 fl67 kk.fgf.98.56.n fl67 bgdgdfg.hjj.879.d fl66 kl..hfh.76.ghg fl66... (5 Replies)
Discussion started by: Padavan
5 Replies

3. Shell Programming and Scripting

Pick the column value based on another column using awk or CUT

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Please someone help me to resolve this issue. Source column1 column2 column3 column4... (2 Replies)
Discussion started by: Ganesh L
2 Replies

4. Shell Programming and Scripting

using AWK to make four column to one column

Gurus, I have file contain following line. ,0113955056,,XAgent-Suspend ,0119418233,,XAgent-Suspend ,0102119078,,XAgent-Suspend I want to make it one column file. How to do this using awk? Can anyone help with 'awk' 0113955056 0119418233 0102119078 (5 Replies)
Discussion started by: thepurple
5 Replies

5. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

6. UNIX for Dummies Questions & Answers

Cut and read

Hi, I have a file which contains comma separated values ex: abc, bdc I need to read this file and provide each value(one by one) as a input to other function. Can some one helpme... -Vinodh' Kumar (1 Reply)
Discussion started by: vino_hymi
1 Replies

7. Shell Programming and Scripting

Help with egrep or grep command to meet multiple criteria

Hello, I"m a newbie :). I hope I can learn from the scripting expert. I'm trying to use egrep and grep commands to get the total count by meeting both criteria. So far, I haven't been able to do it. if robot = TLD and barcode = AA, then final count should be 2 if robot = TLD and... (9 Replies)
Discussion started by: MinBee
9 Replies

8. Shell Programming and Scripting

read file column and paste it in command

Hi Unix gurus I have a file containing 2 coloumns. I would like to do a script which reads the lines and executes a command like this: command <field1> parameters <field2> some more parameters Please let me know how you would do this without AWK, SED or any other mini language (for special... (5 Replies)
Discussion started by: BearCheese
5 Replies

9. Shell Programming and Scripting

How to read arguments to make command

In the make file update updateq: ------------------- ---------- i want the makefile to display some messages when user gives "make update", but totally quite wehn user enters "make updateq". Can u tell me how to read these argument in makefile. $1 doesnt work:( (3 Replies)
Discussion started by: vikashtulsiyan
3 Replies

10. Shell Programming and Scripting

Strange behavior from 'read' statements. (ksh - but could be same on other shells)

I'm getting rather frustrated with an interactive script I'm writing. The script is divided up, with section for setting variable at the top, then functions (which make up most of the script) then basically a line at the end which calls the first function- the program moves between the... (5 Replies)
Discussion started by: alexop
5 Replies
Login or Register to Ask a Question