LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to distinguish between tab and space in a file (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-distinguish-between-tab-and-space-in-a-file-870148/)

akhand jyoti 03-22-2011 01:50 AM

how to distinguish between tab and space in a file
 
suppose in tab separated file with n columns and if in a column i put a value which is space separated .how can i able to find out a specific column..ie

NAME<TAB>ADDRESS<TAB>AGE
akhand jyoti<TAB>banglore<TAB>21
vijay<TAB>new delhi<TAB>22
ram kumar verma<TAB>ram nagar<TAB>43
..
..

now i want to print NAME column(or any column).
how can i do that?
thanks in anticipation!

retxed 03-22-2011 02:06 AM

I think "cut" command that is a filter in Linux does exactly what you are desiring.....It by default takes TAB as a delimiter.
Look into it....


Quote:

cut -f 1,2 <filename>
will list the names and city.

Hope it helps!!

EricTRA 03-22-2011 02:08 AM

Hello,

Have a look at the man page for awk:
Code:

man awk
If you're sure that data are divided by tabs, use \t as field separator and print the desired data.
Code:

awk -F"\t" '{ print $1 }' yourfile
will give you the first column as output. Is that what you're looking for?

Kind regards,

Eric

Telengard 03-22-2011 02:12 AM

I think you want the cut command.

Code:

foo$ cut -f 1 < addresses.txt
NAME
akhand jyoti
vijay
ram kumar verma
foo$ cut -f 2 < addresses.txt
ADDRESS
banglore
new delhi
ram nagar
foo$ cut -f 3 < addresses.txt
AGE
21
22
43
foo$

:study: http://www.gnu.org/software/coreutil...cut-invocation

It will work fine as long as your fields are tab delimited and your needs are simple.

If you want to do very complex text transformations then you should look into gawk.

:study: http://www.gnu.org/software/gawk/man...ode/index.html

HTH


All times are GMT -5. The time now is 11:04 AM.