Sponsored Content
Top Forums Shell Programming and Scripting Problem with awk and if statement Post 302256028 by radoulov on Friday 7th of November 2008 05:14:35 PM
Old 11-07-2008
OK,
could you run the following commands (see example below) with your files and post the entire terminal output?
You should preserve the formatting!

1.
Code:
ls -l

2.
Code:
awk 'NR == FNR { ref[$1] = $2; next }
{ 
  if (substr($2, 1, 3) in ref) { 
    dest = ref[substr($2, 1, 3)] "/"
    system("[ -d " dest " ] || mkdir " dest) 
    print > (dest FILENAME)
    close(dest FILENAME)
    }
  else {
    dest = "others/"
    system("[ -d " dest " ] || mkdir " dest)
    print > (dest FILENAME)
    }
}' FS='[.],' Ref.tmp FS='","' REG_InputFileName

3.
Code:
ls -lR

Something like this:

Code:
% ls -l
total 8
-rw-r--r-- 1 radoulov radoulov 207 2008-11-07 23:09 Ref.tmp
-rw-r--r-- 1 radoulov radoulov 608 2008-11-07 23:08 REG_InputFileName
% 
% 
% awk 'NR == FNR { ref[$1] = $2; next }
{
  if (substr($2, 1, 3) in ref) {
    dest = ref[substr($2, 1, 3)] "/"
    system("[ -d " dest " ] || mkdir " dest)
    print > (dest FILENAME)
    close(dest FILENAME)
    }
  else {
    dest = "others/"
    system("[ -d " dest " ] || mkdir " dest)
    print > (dest FILENAME)
    }
}' FS='[.],' Ref.tmp FS='","' REG_InputFileName
% ls -lR
.:
total 24
drwxr-xr-x 2 radoulov radoulov 4096 2008-11-07 23:09 IRQ
drwxr-xr-x 2 radoulov radoulov 4096 2008-11-07 23:09 JOR
drwxr-xr-x 2 radoulov radoulov 4096 2008-11-07 23:09 others
drwxr-xr-x 2 radoulov radoulov 4096 2008-11-07 23:09 PAK
-rw-r--r-- 1 radoulov radoulov  207 2008-11-07 23:09 Ref.tmp
-rw-r--r-- 1 radoulov radoulov  608 2008-11-07 23:08 REG_InputFileName

./IRQ:
total 4
-rw-r--r-- 1 radoulov radoulov 38 2008-11-07 23:09 REG_InputFileName

./JOR:
total 4
-rw-r--r-- 1 radoulov radoulov 38 2008-11-07 23:09 REG_InputFileName

./others:
total 4
-rw-r--r-- 1 radoulov radoulov 38 2008-11-07 23:09 REG_InputFileName

./PAK:
total 4
-rw-r--r-- 1 radoulov radoulov 38 2008-11-07 23:09 REG_InputFileName
%

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

if statement problem

I keep getting an error at line 21, it doesn't like my if statement. Previously I have tried using (( )), but still get errors. The current error is that server_busy is not found. This is the script: #! /bin/ksh server_busy="na" for file in $1 $2 $3 $4 $5 $6 do echo " ${file}\t\c" ... (1 Reply)
Discussion started by: coughlin74
1 Replies

2. UNIX for Dummies Questions & Answers

if statement problem

hi all. i just have a very small problem. i have a menu of 7 choices. i want an if statement so that if the user chooses anything except inside the 1 to 7 range, i can handle the error for it. i tried this: if ] then ....... fi (but it dont work) ...any suggestions? ... (4 Replies)
Discussion started by: djt0506
4 Replies

3. UNIX for Dummies Questions & Answers

if statement problem

See https://www.unix.com/shell-programming-scripting/96846-if-statement-problem.html (0 Replies)
Discussion started by: f_o_555
0 Replies

4. Shell Programming and Scripting

if statement problem

Hi I have a bash script like this if then echo "A" else echo "B" fi $1 is something like 02350 (there is always a trailing '0') and I would like to have an if based on the value of the digits after the 0. Can anybody help? Thanks, Sarah (3 Replies)
Discussion started by: f_o_555
3 Replies

5. Shell Programming and Scripting

problem with if/while statement

I'm trying to have the script check if a file has data or not, and then process it accordingly. If the file is empty, I want it to return "nothing to do", if not, I want it to process the file line by line. This is what I have so far, but it always returns "nothing to do", even if the file is not... (4 Replies)
Discussion started by: ddrew78
4 Replies

6. UNIX for Dummies Questions & Answers

Having problem with if statement

Could someone help me out with this if statement? It's supposed to get a person's website, but it isn't working when I run it. website="" echo "Would you like to enter a website? Enter Yes/No" read choice if then while do echo "Please enter a website:"; read... (4 Replies)
Discussion started by: Sotau
4 Replies

7. Shell Programming and Scripting

if statement problem

Writing my script and I'm banging my head on the desk right now ... My biggest problem is the 3rd IF statement where I check if the username exists. Doing the grep command on it's own in the shell gives me a 1 or 0 value. Running the script, it always returns a false value (runs the ELSE... (4 Replies)
Discussion started by: ADay2Long
4 Replies

8. Shell Programming and Scripting

Awk or If/statement Calculation Problem

#!/bin/sh CURRENTSTATE=2 CSVCSTATE=2 LASTSTATECHANGE=8 CSVCSTATEAGE=5 if (($CURRENTSTATE==$CSVCSTATE))&&(($LASTSTATECHANGE>=$CSVCSTATEAGE)) echo GREAT fi returns: ./aff: line 12: syntax error near unexpected token `fi' ./aff: line 12: `fi' what am i doing wrong here? (6 Replies)
Discussion started by: SkySmart
6 Replies

9. Shell Programming and Scripting

Awk/sed problem to write Db insertion statement

Hi There, I am trying to load data from a csv file into a DB during our DB migration phase. I am successfully able export all data into a .csv file but those have to rewritten in terms insert statement which will allow for further population of same data in different DB My exiting csv record... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

10. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies
STRCAT(3)						     Linux Programmer's Manual							 STRCAT(3)

NAME
strcat, strncat - concatenate two strings SYNOPSIS
#include <string.h> char *strcat(char *dest, const char *src); char *strncat(char *dest, const char *src, size_t n); DESCRIPTION
The strcat() function appends the src string to the dest string, overwriting the null byte ('') at the end of dest, and then adds a ter- minating null byte. The strings may not overlap, and the dest string must have enough space for the result. The strncat() function is similar, except that * it will use at most n characters from src; and * src does not need to be null-terminated if it contains n or more characters. As with strcat(), the resulting string in dest is always null-terminated. If src contains n or more characters, strncat() writes n+1 characters to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1. A simple implementation of strncat() might be: char* strncat(char *dest, const char *src, size_t n) { size_t dest_len = strlen(dest); size_t i; for (i = 0 ; i < n && src[i] != '' ; i++) dest[dest_len + i] = src[i]; dest[dest_len + i] = ''; return dest; } RETURN VALUE
The strcat() and strncat() functions return a pointer to the resulting string dest. CONFORMING TO
SVr4, 4.3BSD, C89, C99. SEE ALSO
bcopy(3), memccpy(3), memcpy(3), strcpy(3), string(3), strncpy(3), wcscat(3), wcsncat(3) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2010-09-20 STRCAT(3)
All times are GMT -4. The time now is 01:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy